This code:
>>>
my $ref = {'eggs' => 'bacon'};
print $ref->{eggs}, "\n";
print $ref, "\n"; # prints $ref as a hex number
print ($ref)->{eggs}, "aaaaaaaaaa\n"; # also prints $ref as hex number (why?)
>>>

outputs:
>>>
bacon
HASH(0x176f054)
HASH(0x176f054)
>>>

on both perl 5.005_03 for UNIX, and ActivePerl 5.6.0 build 623.

Am I reading it wrong?  It seems like
$ref->{eggs}
and
($ref)->{eggs}
ought to look the same when printed, but they don't.  "->" requires a left
operand, so it ought to remove any ambiguity about what the line means.

There are times when you'd need the latter form, such as when a reference
is obtained by a function call, as in this code:

>>>
sub ReturnRef
{
        my $ref = {};
        $ref->{eggs} = 'bacon';
        return $ref;
}
print (ReturnRef)->{eggs};
>>>
which looks to me like it ought to print "bacon", but actually prints
HASH(0x176f054).

You *can* do the following:
>>>
sub ReturnRef
{
        my $ref = {};
        $ref->{eggs} = 'bacon';
        return $ref;
}
print ((ReturnRef)->{eggs});
>>>
and this will print "bacon" instead of "HASH(0x176f054)", since apparently
the parentheses around "(ReturnRef)->{eggs}" make it work.  But it doesn't
seem like they should be necessary.

And in any case, how come
print ($ref)->{eggs}, "aaaaaaaaaa\n";
doesn't print out the "aaaaaaaaaa\n" at the end, just the hex hash value?

Is this a specific case of some general source of confusion that is
addressed in a FAQ?

        -Bennett

[EMAIL PROTECTED]     http://www.peacefire.org
(425) 649 9024
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to