Symbolic references are a lot like hash keys (really like them if you dig down far enough). So if you have:
use warnings; use strict; no strict 'refs'; my %hash = ( badger => 'nadger', mutt => 'nut' ); # Package variable, named badger our $badger = 'furry'; # Lexical variable - gives a warning that it is shadowing # the package var my $badger = 'stripey'; my $str = 'badger'; # The value of $str is used to lookup in the hash # Hence this prints 'nadger' print $hash{$str}, "\n"; # With strict refs off, we can use '$str' as a reference and # so this prints 'furry', the value of the $badger package var print ${$str}, "\n"; # This is a normal variable lookup, whose value will be # 'stripey', since the lexical is shadowing the package var # in this scope. print $badger, "\n";
then you can see that using a symbolic reference is a bit like looking something up in an un-named hash. In fact, this is almost exactly what is going on, since the package variables are stored in a hash associated with the package. (The only difference is that there is an extra layer of indirection, to sort out the difference between things with different types but the same name: $badger, @badger, %badger etc.)

This means that symbolic references have similar properties to hash keys, in particular perl can't check at compile time whether you have mis-spelled the variable name, instead it will just look up the wrong name give you an undef value.

Also as you can see, symbolic references don't really have much advantage over hashes, if you think you want a symbolic reference, it is quite likely a hash will do the job for you.

And lastly, emulating a structure by using named keys in a hash shares some of the disadvantages of symbolic references. If you're using a hash reference to implement an object you can get compile-time checking back again by using accessors, made easy by handy modules such as Class::Accessor.


In reply to Re: Symbolic References by jbert
in thread Symbolic References by brickwall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.