Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: question about variabies/references (ignore my previous botched entry)

by chromatic (Archbishop)
on Apr 05, 2000 at 19:04 UTC ( #6941=note: print w/ replies, xml ) Need Help??


in reply to question about variabies/references (ignore my previous botched entry)

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!


Comment on Re: question about variabies/references (ignore my previous botched entry)
Select or Download Code
RE: Re: question about variabies/references (ignore my previous botched entry)
by btrott (Parson) on Apr 05, 2000 at 20:12 UTC
    You (chromatic) wrote:
    > 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";
    No, I don't think this is true. When you do
    $main::{$test} = "skot2";
    you're manipulating the symbol table of package main; this is a very different thing than modifying a scalar variable ($main::scott). $main::scott and $main::{"scott"} are two different things.
      You're right, the post-interpolated line should read: *main::scott = "scot2"; Here's what I've been using to test my assumptions:
      #!/usr/bin/perl $test1 = "value of test1"; $test2 = "test1"; $test3 = "Why are you here?"; # refers to *main::test1 print "=>$main::{$test2}<=\n"; print "Original \$test1: ...$test1...\n"; $main::{$test2} = "test3"; # $main::{"test1"} = "test1"; print "\$test1 = >>", $test1, "<<\n"; print "Main package: ", $main::test1, "\n";
      As you can see, $main::{$test2} evaluates to a typeglob. Saying that $main::{$test2} = "test3" changes the scalar is technically incorrect. (Though it is what my previous answer would lead one to believe!)

      Dunno where that u came from. Maybe it's a mu?

        chromatic wrote:
        > As you can see, $main::{$test2} evaluates to a typeglob.
        Right; because a typeglob *is* an entry in the symbol table.

        > Saying that $main::{$test2} = > "test3" changes the scalar is technically incorrect.
        Sorry, did I say that?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://6941]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-08-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls