Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by plaid (Chaplain)
on Apr 05, 2000 at 12:31 UTC ( [id://6933]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found