develooper Front page | perl.beginners | Postings from December 2002

Re: Scalar reference, pointer to pointer meaning

Thread Previous | Thread Next
Rob Dixon
December 28, 2002 04:11
Re: Scalar reference, pointer to pointer meaning
Message ID:
Hi Gary

You're suffering a little C-lag. The backslash performs roughly the same
function as the ampersand in C, while dereferencing requires knowing the
type of the reference, which can be discovered using the ref() function.

    my (@array, $scalar, $lp);

    $lp = \@array;
    print ref $lp;              # prints ARRAY
    @$lp = (0, 1, 2);           # same as @array = (0, 1, 2)

    $lp = \$scalar;
    print ref SCALAR;           # prints SCALAR
    $$lp = 99;                  # same as $scalar = 99

See in-line.

"Gary Hawkins" <> wrote in message
> I have a variable using module Win32::API that would be a pointer to a
> in C.
> If I do:
> $var = $lplpBuffer;
> print  "\$var $var\n";

You're copying the pointer $lplpBuffer into $var, which you claim is a
reference. You then print it out, giving
'$varʦ?XXXXXXXXXXXXXXXXXXXXXXXXXXXX' (below) which means that $lplpBuffer
wasn't a reference at all (otherwise it would have printed '$var
SCALAR(0x12345678)' or similar) but is more likely to be the buffer itself?

From now on, let's assume that $lplpBuffer really is what you believe it to

> $var = \$lplpBuffer;
> print  "\$var $var\n";

You're taking a reference to $lplpBuffer and putting it into $var. In your
naming scheme this would be called $lplplpBuffer. This prints out as I would
have expected.

> $var = \$lplpBuffer[0];
> print  "\$var $var\n";

You're taking element zero of a non-existent array @lplpBuffer, taking a
reference to that and putting it into $var. Without 'use strict' Perl will
create the missing array for you and give it an undefined element. Thus $var
is the same type as in the previous case, but pointing to a different
variable. Hence the printout is the same with a different hex value.

> prints:
> $var SCALAR(0x193b0dc)
> $var SCALAR(0x183f124)
> What it points to should contain a string.
> How can I get that string?

Well, the answer is something like

    print $lplpBuffer;

but I think you're going about it the wrong way. Something else in the
module will know how much of the buffer is in use, and there's more than
likely to be an object method to extract it. Let us see the code you're
using and we may be able to help further.



Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About