----- Original Message ----- 
From: "Nicholas Clark" <[EMAIL PROTECTED]>
To: "Sisyphus" <[EMAIL PROTECTED]>
Cc: "inline" <inline@perl.org>
Sent: Monday, June 20, 2005 8:50 PM
Subject: Re: Accessing info contained in objects


> On Mon, Jun 20, 2005 at 08:35:55PM +1000, Sisyphus wrote:
>
> > The Inline C function 'array_print()' prints out the values "contained"
in
> > the 2 objects.
> > How do I get direct access to those values "contained" in $z1 and $z2
using
> > perl (as opposed to the Inline C 'array_print()' function) ?
>
> Not really an Inline question :-)
>

I was hoping you wouldn't notice - and at least it's a perl question (unlike
all of those C questions I've asked in the past - which were most often
answered by Sam Tregar. I often wonder whether that's why he left... :-)

>
> use Config;
> sub pp_array_print {
>     my $self = shift;
>     my $size = shift;
>     my $address = $$self;
>     # Caution. 1: Get the address wrong and this will segv
>     # "L!" isn't guaranteed to be the same size as a pointer. This will
end
>     # up being platform specific
>     my $array = unpack "P" . ($Config{longsize} * $size), pack "L!",
$address;
>     foreach (unpack "L!*", $array) {
> printf "%u ", $_;
>     }
>     print "\n";
> }
>
> pp_array_print($z1, $size);
>

Oh yuck ... I feared it was going to involve pack() and unpack(), which is
not so bad when it comes to long ints - though, as is blatantly obvious, bad
enough to fool me. But in real life it's an array of structures created by a
third party library, and I need both read and write access. I already have
the XS routines to access those structures individually, and I was hoping to
use those routines on the structures "contained" in these "array objects". I
figured that if I could do it with longs, I'd be able to extend that to the
structs .... but now I'm not so sure.
Instead I'll probably end up writing additional Inline C routines to get the
access I desire - which is a tedious solution, but relatively
straightforward. (Otoh, it would be a good learning exercise if I stuck to
my original plan - assuming it's do-able - and also assuming my brain is
flexible enough to assimilate whatever it is that's required.)

Thanks Nicholas.
pack() and unpack() always confuse me. That's a useful demo - and one that
I'll keep hold of. (Even now, I can't see how pp_array_print() works ...
but, of course, it does :-)

Cheers,
Rob

Reply via email to