On Wed, Sep 28, 2011 at 1:00 PM, lesleyb <lesl...@herlug.org.uk> wrote:
> $Policy->{INSTNCE} evaluates to a hash.  Then you ask Perl to access the value
> given by the key $CondID in that hash.  $CondID is a key to a reference to
> another anonymous hash.  But this time you don't use the '->' to get to the
> value of the DESCRPT key.

Only the first -> is needed. After that, Perl knows what to do
without it.

#!/usr/bin/perl

use strict;
use warnings;

my $foo = {
    bar => {
        baz => 'FTW'
    }
};

print "Perl $foo->{bar}{baz}!\n";

__END__

> In your data strucure $Policy->{INSTNCE}, INSTNCE is a key to a hash hence the
> HASH(...) part of the result.    %Instance is also a hash, not a reference to
> one, so $Instance{$CondID} should evaluate to a reference to the anonymous 
> hash
> pointed to by $CondID.  So you really shouldn't get to the value of
> $CondID->{DESCRPT} without that '->'.

Same thing applies here.

#!/usr/bin/perl

use strict;
use warnings;

my %foo = (
    bar => {
        baz => 'FTW'
    }
);

print "Perl $foo{bar}{baz}!\n";

__END__

> I suspect this result is one of Perl's 'doing the right thing' moments.

More or less. I think that the reason that it works is that it
isn't ambiguous. At that point in the syntax there's nothing else
that it could mean so Perl just knows that you must be
dereferencing a reference... I think. :P The same applies to
array references and sub references.

> 2.  the hashes/anonymous hashes have been defined in reverse order to yours, 
> it
>    would not have been possible to run the code under the constraints of (1)
>    above otherwise.
>    Which may indicate your data structure reduction given above is inaccurate.
>    Or that you have not been using strict and warnings otherwise you'd have
>    picked up the issue with the order of declaration/definition of the hashes.

Agreed. Technically it could have worked if he had
"forward-declared" the variables earlier in the code, but that
would probably be the wrong thing to do anyway.


-- 
Brandon McCaig <http://www.bamccaig.com/> <bamcc...@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to