* Bill Gradwohl <[EMAIL PROTECTED]> [2006-01-11T10:07:10]
> On Wed, 2006-01-11 at 09:25 -0500, Ricardo SIGNES wrote:
> > @{ $children{$v} } means:
> >   get the scalar stored in $children{$v} (which is an entry in
> >   %children) and dereference it as an array
> > In other words:
> >   my $entry = $children{$v};
> >   @$entry;
> 
> First, Thank You for your reply.

You're quite welcome!

> I thought about that as a possibility, but rejected it because I never
> saw the %children hash ever get initialized with even a single key/value
> pair. As far as I can tell, $children{anything at all} is undef. I know
> that's incorrect since the code works, but I can't see how it works. I
> understand that autovivification is creating things on the fly as
> needed, but see no mechanism to supply a key/value pair.

  use strict;
  use warnings;

  use Data::Dump::Streamer;

  my %children;

  push @{ $children{isaac} }, qw(jacob esau);

  Dump(\%children); 

This will output, more or less: $HASH1 = { isaac => [ 'jacob', 'esau' ] };  

> The push is extracting a value to be used as an array reference, but to
> extract a value, one must be there to begin with and I can't see how a
> value was ever supplied.

It is not extracting a value, it is asserting that a value, specifically an
array reference, exists.  Since there is nothing to prevent that from becoming
true, it does.  The author says, "let there be an arrayref," and there is, and
it is good.

This, however, would not be good:

  my %children = ( joe => '' );
  push @{ $children{joe} }, qw(betty veronica);

You see, undef can be used as an array ref -- it becomes [].  "" can't be used
as an array reference, and neither can a lot of things -- most, in fact.  Only
undef and an array reference can be treated as an array reference, and once
you treat undef like an array reference by pushing to it, it ceases to be
undef.

Autovivification is a great thing, but it does sometimes cause a bit of
consternation.  Good luck!

-- 
rjbs

Attachment: pgpGvfH71N8Wg.pgp
Description: PGP signature

Reply via email to