This has to be one of the best questions I've seen here, if only for the reson that it gave me a good chance to go digging around and try to figure this out. This explanation is far from perfect and any additions/corrections are welcome.

The main problem comes with the line $main::{$test} = "skot2". This is a lookup into the symbol table of main::, with the key of $test. Perl stores its symbol table in the form of string keys, and typeglob values. $main::{$test} is not the same as the $$test a couple lines down, but rather is the same as $::{$test}.

As for the weirdness in the last line, with $scott and $skot2 now being the same value, the $main::{$test} = "skot2" line again needs to be looked at. The main:: symbol table is expecting a typeglob or a reference on assignment, but when perl is expecting a typeglob and gets a string, it automatically creates a new typeglob of that name, so that line is functionally equivalent to having put $main::{$test} = *skot2. This is effect creates an alias between $scott and $skot2, so changes to either one will affect the other.

What needs to be done to get any kind of sensible answer is to pass in a reference on the assignment to $main::{$test}. There are two ways to go about it. Either you can do $main::{$test} = \"skot2", which will make the value of $scott come out right, but will make it be a constant, and thus the assignment to $$test will fail. The better way to do it would be to assign the value "skot2" to another variable, and pass in the reference of that variable.

Hope this helps.


In reply to Re: question about variabies/references (ignore my previous botched entry) by plaid
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.