[Pdx-pm] Re: Pointer.pm

Brian Ingerson ingy at ttul.org
Fri Mar 12 12:18:22 CST 2004


On 11/03/04 13:10 -0800, Ovid wrote:
> I've been playing with Pointer.pm and am trying to figure out what I
> *think* should be simple.  Let's say I have a scalar with an integer:
> 
>   my $x = "foo";
>   $x = 42;
> 
> Getting the string is simple:
> 
>   print pointer->of_scalar($x)->get_pointer->get_pointer->get_string;
> 
> I can't figure out how to get the 42 back, though.  The internals are
> straightforward:

The integer is the IVX below. It's the 4th integer in the set.

  use Pointer;
  use Pointer::int;
  my $x = 42;
  print ((pointer->of_scalar($x)->get_pointer('int') + 3)->get);

That works. In the next rev of Pointer.pm I'd like it to be like this:

  use Pointer; # Pointer::int is inside Pointer.pm
  my $x = 42;
  print pointer->of_scalar($x)->get_pointer('int')->get(3); # get takes offset

So the breakdown of the first program is like this.

  # Use the class for regular pointers (void *)
  use Pointer;
  # Use the class for integer pointers
  # Note. As shown above, I think all basic C types should be inside
  # Pointer.pm
  use Pointer::int;
  # create an integer scalar
  $x = 42;
  # create a pointer to the sv. since pointer() has no arguments passed
  # in, it returns a pointer to void. ie of *type* void
  my $sv_pointer = pointer->of_scalar($x);
  # get a pointer (to int) of the xpviv. this pointer will have a type
  # of int which is important.
  my $xpviv_pointer = $sv_pointer->get_pointer('int');
  # point past the first 3 integers
  my $ivx_pointer = $xpviv_pointer + 3;
  # get the integer
  my $integer = $ivx_pointer->get;

Simple eh?

Cheers, Brian

PS Nice diagram

>                 sv     xpviv
>   +---------------+   +-----+
>   | ANY           |-->| PVX |-->(the array with the string)
>   +---------------+   +-----+
>   | REFCOUNT      |   | CUR |
>   +---------------+   +-----+
>   | FLAGS  | TYPE |   | LEN |
>   +---------------+   +-----+
>                       | IVX |
>                       +-----+
> 
> In short, how do I get the second struct's IVX value with Pointer.pm?
> 
> Cheers,
> Ovid  
> 
> =====
> Silence is Evil            http://users.easystreet.com/ovid/philosophy/indexdecency.htm
> Ovid                       http://www.perlmonks.org/index.pl?node_id=17000
> Web Programming with Perl  http://users.easystreet.com/ovid/cgi_course/



More information about the Pdx-pm-list mailing list