Here's a funny thing. $scott and $main::scott refer to the same variable, assuming you haven't declared that you're in another package. Another piece of the puzzle is that putting curly braces around anything that looks like a variable causes Perl to interpolate the value of that variable. Doing: $main::{$test} = "skot2"; first causes interpolation -- so that the interpreter now has: $main::scott = "skot2"; From there, I assume plaid is correct in that the interpreter decides that you really meant to do a typeglob assignment, and takes "skot2" to be a symbolic reference (that is, the name of another variable). Since it hasn't been declared before, $scot2 (and $main::scot2) are autovivified. Next, $main::scott is aliased to $scot2. The $$test line is even more tricky. You might also write it ${$test} = "surprise!"; to be consistent with my explanation. If $test were a normal reference, this would dereference it. Since it contains the name of a variable ($skot2 has just been created automatically), Perl considers it a symbolic reference, looks up $main::skot2 in the symbol table, and assigns the value of "surprise!" to it.

Because $main::scott is aliased to $skot2, accessing it gets you the same value ("surprise!"). Because you're in package main, you can leave off the $main:: portion, and accessing $scott also gets you your "surprise!".

These are three or four things that will eventually surprise any new Perl programmer. Using strict and -w will warn you when these things happen (except for autovivification in hashes, which is worth another page of explanation). Use them liberally!


In reply to Re: question about variabies/references (ignore my previous botched entry) by chromatic
in thread question about variabies/references (ignore my previous botched entry) by howard40

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.