mce has asked for the wisdom of the Perl Monks concerning the following question:

All,

I was reviewing some code from a coworker (no, not my code :-) ), and I found a severe perl error, but the code works fine.
Now, I was wondering, why does it actually work. This is his code reformatted

$a{test}="very "; $a{test}{this}="strange"; print $a{test}; print $a{test}{this};

Which results in "very strange"
First of all, strict does not allow this with the message (note:I always use strict!!)
Can't use string ("very ") as a HASH ref while "strict refs" in use ..
If I rewrite this code, it actually does this:
$a{test}="very "; $a{test}->{this}="strange"; print $a{test}; print very->{this}; #### ?????
So "very" is the bareword that is a reference to a hash, but why does it work???
He claims, "it works, so I won't change it".
What even works is
delete $a{test}
But I am afrait that this does not clear up memory (is this correct?)
Is this one of the many mysteries of perl?
Please no replies with comments about "use strict" because know about that, I just want to know why the bareword reference works.
---------------------------
Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium

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

    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
      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