in reply to bareword and hash reference

Jep, here is something strange going on. let's send the following to the debugger:

use vars qw/%x/; $x{test} = "very "; $x{test}{this} = "strange"; print $x{test}; print $x{test}{this};

the second asignment causes %x not to be changed in any way. But if you make a 'V' after it, you'll see a new hash called 'very' as a symbol available with key 'this' and value 'strange'. So what's actually happening is kind of autovivified symbolic hash-refence assignment. I suppose the docs explain it somewhere but ...

Anyway: your coworker is affecting the symbol table and that is dangerous for the code. What happens if:

$x{test} = 'ENV'; $x{test}{important_key} = 'bad value';

Yes, it clobbers the environmental variable!

--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: bareword and hash reference
by mce (Curate) on Aug 22, 2002 at 14:24 UTC
    Hi,

    Yep, you are correct, and this means that

    $a{test}="ENV"; $a{test}{this}="strange"; die if ENV->{this} eq $ENV{this};
    this dies??

    I really need some coffee now.
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

      Exactly. I conclude 'string -> {key}' equals '$string{key}' and is a symbolic reference, for 'string' is an at runtime determined name, and that's bad.

      --
      http://fruiture.de

        fruiture++

        Using Deparse shows that that's just what happens:

        perl -MO=Deparse scratch.pl $a{'test'} = 'very '; $a{'test'}{'this'} = 'strange'; print $a{'test'}; print $very {'this'};