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

I've just found any string may be used as reference! Is anybody know where this behaviour explained in documentation? Or this is a bug? This code:
$qqq = undef(); $www = undef(); $qqq->{qwe} = 123; printf "'%s' '%s' '%s' '%s'\n", $qqq, $www, $qqq->{qwe}, $www->{qwe}; $eee = ''; $rrr = ''; $eee->{asd} = 456; printf "'%s' '%s' '%s' '%s'\n", $eee, $rrr, $eee->{asd}, $rrr->{asd}; $ttt = 'Test'; $yyy = 'Test'; $ttt->{zxc} = 789; printf "'%s' '%s' '%s' '%s'\n", $ttt, $yyy, $ttt->{zxc}, $yyy->{zxc};
output:
'HASH(0x81551c8)' 'HASH(0x81552d0)' '123' '' '' '' '456' '456' 'Test' 'Test' '789' '789'

Replies are listed 'Best First'.
Re: string as ref ?!?!
by Abigail-II (Bishop) on May 10, 2004 at 10:44 UTC
    It's documented exactly where you expect it be documented: in the manual page about references: man perlref.

    Abigail

      Yeah, I've already realized what this is just symbolic reference... I've doublechecked `perldoc perlref`, and now, when I know what to search for I've found only one sentence about this issue: "Using a string or number as a reference produces a symbolic reference, as explained above.". And there in nothing more about this issue "above". :-( Anyway, I think a couple of examples like:
      "Test"->{qwe} = 111; "Test"->[1] = 222;
      will be nice to have in perlref...
        Examples from perlref dealing with symbolic references:
        $name = "foo"; $$name = 1; # Sets $foo ${$name} = 2; # Sets $foo ${$name x 2} = 3; # Sets $foofoo $name->[0] = 4; # Sets $foo[0] @$name = (); # Clears @foo &$name(); # Calls &foo() (as in Perl 4) $pack = "THAT"; ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval

        Abigail

        I've already realized what this is just symbolic reference

        No, it isn't. A symbolic ref uses the string in the variable as the name of a package variable:

        my $f = "foo"; our $foo = "blah"; print $$f; __OUTPUT__ blah

        The behavior you're seeing in your example code is autovivification. You tell Perl that you want a hashref, so you get one. This behavior is forbidden by use strict 'refs'. For that, you should explicitly put a hashref into the variable, such as with my $var = { }; or  my $var = \%hash;.

        ----
        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Re: string as ref ?!?!
by Hena (Friar) on May 10, 2004 at 10:46 UTC
    If you use "use strict" then perl will catch those. If you notice the second groups rrr reference prints a value that has not even been set. I don't know how perl handles these situations without use strict, so i don't know why this happens.
      The second example works exactly as the third example. It doesn't matter what string we use - an empty string works the same as a string with a different value.
      "" -> {foo}
      is the same as
      ${""}{foo}
      just like
      "Bar" -> {foo}
      is the same as
      ${"Bar"}{foo}

      Abigail