Paolo Gianrossi wrote:

Here's my problem (well, a shortened example=)

#hashes.pl
use strict;
use warnings;

use Data::Dumper;

my %a=(a=>1, b=>2, c=>3);
print Dumper \%a;

print "\n".("-" x 10)."\n";

my $c=$a{d}->[0];
print Dumper \%a;

$ perl hashes.pl

$VAR1 = {
          'c' => 3,
          'a' => 1,
          'b' => 2
        };

----------
$VAR1 = {
          'c' => 3,
          'a' => 1,
          'b' => 2,
          'd' => []
        };


Now, while I'd expect a warning on the line of my $c=$a{d}->[0]; (like,
dunno, you're trying to dereference undef?) I'd never think a field is
added to the hash...

I'm confused.. Is this expected behaviour? Changing a right-hand operand
(without calling a sub)?

It is called 'autovivication'. If you try to use the value of a non-existent hash or array element as if it was a reference to a hash or array then an anonymous hash or array will be created for you. Take a look at

  perldoc perlglossary

and look at 'autovivification'.

Your example is not a very useful one. It is most often seen as

  push @{$a{d}}, $newdata;

or something similar.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to