[tpm] dereferencing anonymous hashes

Rob Janes janes.rob at gmail.com
Tue Apr 10 13:21:35 PDT 2007


ouch.

try putting on use warnings when you do my %x = {};

also, dumping out %x should show a funky key with an undef value.

my %device = {};  ## this is the same as my %device = ( { } => undef );
$config{$cfg}{device} = \%device;  ## a reference to a hash with a reference
for a key
$device{$devid}{status} = 'OK';  ## this is ok, auto creates a hash within a
hash

you really should use

use warnings;
use strict;

at the top of your perl scripts.  it will help catch your errors before they
catch you.

On 4/10/07, Fulko Hew <fulko.hew at gmail.com> wrote:
>
> On 4/10/07, Rob Janes <janes.rob at gmail.com> wrote:
> > you're a php guy, right?
>
> Nope.  Never seen PHP in my life... ain't gonna start now!
>
> >  $x = (); is a list assignment to a scalar.  scalar conversion results
> in
> > the scalar ($x that is) being assigned the count of the number of items
> in
> > the list.  0 or zero.
> >
> >  $x = {}; assigns a reference to an anonymous hash.
>
> OK, so I was getting frustrated and made a whole lot of transcription
> errors to boot. :-(
>
> The question should have been... whats the difference between:
>
> %x = ();  and
> %x = {};
>
> because what fixed my problem was (transcribing correctly this time):
>
> my %device = {};
> $config{$cfg}{device} = \%device;
> $device{$devid}{status} = 'OK';
>
> ...
>
> my %device = %{$config{$cfg}{device}};
> $status = $device{$devId}{status};
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/toronto-pm/attachments/20070410/5d6f10a4/attachment.html 


More information about the toronto-pm mailing list