I wonder if we have an array as well?....

I was meandering through the Class::Delegation docs when I happened upon this:

a little-known feature of the -> operator is that if a hash access is performed on a string, that string is taken as a symbolic reference to a package hash variable in the current package

Now of course we won't ask where in the Perl docs this is documented, because it probably isn't. But let us give an example of it

use strict; package Mr::Rogers; %Mr::Rogers = ( instrument => 'violin', clothing => 'cardigan', deameanor => 'mild', age => sub { rand 20 + rand 60 } , habitat => 'US-unknown'); map { __PACKAGE__->{cookies_eaten}++ if (rand 10 > 5) } (1..5) ; warn __PACKAGE__->{cookies_eaten};

Replies are listed 'Best First'.
Bad Symref! Naughty!
by dragonchild (Archbishop) on Nov 03, 2001 at 02:29 UTC
    Also known as symbolic references. There are some hundred threads on this site stating why this is a "Bad Idea"(tm). In fact, I think that there's a monk who has, as a sig, "Don't use symrefs unless you know why you shouldn't use symrefs" (or something like that).

    Basically, you're directly accessing the symbol table. This, by itself, isn't bad. But, you're working with global variables that don't even have the safety of the compiler making sure things are ok. Hence, mispellings aren't caught by strict 'vars', etc.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: self-referent hash for your packages
by blakem (Monsignor) on Nov 03, 2001 at 02:57 UTC

      I'll expand on that a bit...

      The syntax for dereferencing a hash reference is identical whether the reference is a symbolic reference (a string that names a global variable) or a real reference ("hard reference"). So if -> works on real references to hashes, then it also works on symbolic references to hashes (that is, on strings).

      You avoid using symbolic references by saying use strict. You don't avoid using symbolic references by avoiding certain types of dereferencing syntax.

              - tye (but my friends call me "Tye")
        a symbolic reference (a string that names a global variable) ... You avoid using symbolic references by saying use strict.
        If you look at my code, I did use strict