in reply to Autovivification and soft refs

print "Case 4: stopper scalar\n"; $x = 'stopper'; exists $x->{'y'}->{'z'}; print Dumper $x;
What's the problem here? Perl is populating the hash %stopper, just as you ordered it to do. Replace the last line with print Dumper \%stopper and you'll see.

Abigail

Replies are listed 'Best First'.
Re: Re: Autovivification and soft refs
by pg (Canon) on Jan 22, 2004 at 04:05 UTC

    Just add a bit of info on top of what Abigail-II said. If you my %stopper, it will stop the autovivification:

    use Data::Dumper; my %stopper; $x = 'stopper'; exists $x->{'y'}->{'z'}; print Dumper $x; print Dumper \%stopper;

    Which prints:

    $VAR1 = 'stopper'; $VAR1 = {};

    Comment out that my %stopper line, you will see the autovivification:

    $VAR1 = 'stopper'; $VAR1 = { 'y' => {} };
      If you my %stopper, it will stop the autovivification:
      Well, that would be strange. Very, very strange. Do you think that happens because there's an explicite check in perl that says "hmmm, we are doing autovivification of a secondary key in the hash, but the parent hash is a lexical, so let's not do it", or that you've hit a strange bug?

      Or could it be that autovivification is happening? And you are just giving the wrong argument to Dumper? Try giving \%{"stopper"} as argument to Dumper.

      Abigail

      Now that's interesting. Your post inspired me to dig a little deeper... it looks like the autovivification is still happening, but not on the lexical %stopper. Instead the global %::stopper is affected. Which makes sense, I suppose, because %::stopper is what was being affected in Abigail's original snippet, and with no lexical on scope, %stopper (i.e. the symbolic reference) was really referring to %::stopper. Very interesting, I certainly learned something. Thanks for pointing that out!

      use Data::Dumper; my %stopper; $x = 'stopper'; exists $x->{'y'}->{'z'}; print Dumper $x; print Dumper \%stopper; print Dumper \%::stopper;

      -- Mike

      --
      XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
      -- grantm, perldoc XML::Simpler