Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

computed symbolic references

by valentin (Abbot)
on Jul 13, 2004 at 13:06 UTC ( #373962=perlquestion: print w/replies, xml ) Need Help??
valentin has asked for the wisdom of the Perl Monks concerning the following question:

Hi, my question is two folded:
1. What is the difference between ${$x."::VERSION"} and ${"$x::VERSION"}?
2. Why is $y undefined in the first example?
3. Why does perl think $y is a filehandle in the second example?
perl -MFile::Find -we '$x='File::Find'; print ${ $y = "$x::VERSION" } +print $y'
perl -MFile::Find -we '$x='File::Find'; print ${ $y = $x."::VERSION" } + print $y'

Replies are listed 'Best First'.
Re: computed symbol references
by gellyfish (Monsignor) on Jul 13, 2004 at 13:16 UTC

    1. The first is appending '::VERSION' to the value of $x whereas the second is going to look for the variable $VERSION in the package 'x' - it will not expand $x separately.
    2. Because there is no variable $x::VERSION defined
    3. Because you missed the semicolon after the first print statement.

    /J\

Re: computed symbol references
by broquaint (Abbot) on Jul 13, 2004 at 13:23 UTC
    1. The first concatenates $x and the string ::VERSION then symbolically dereferences, the second symbolically dereferences $x::VERSION
    2. $y is undefined because $x::VERSION is undefined, perhaps you meant ${ $y = ${ $x . '::VERSION' } }
    3. because you're passing it as the first argument to print which is expected to be a filehandle if it is not preceded by a comma
    If you're interested in dynamically accessing the $VERSION package variable then check out UNIVERSAL::VERSION.
    HTH

    _________
    broquaint

Re: computed symbol references
by dragonchild (Archbishop) on Jul 13, 2004 at 13:21 UTC
    Your first question is easy to answer.
    package x; VERSION = 'bar'; package Foo; VERSION = '1.01'; package main; $bar = '0.01'; $x = 'Foo'; print ${"$x::VERSION"}, $/; print ${$x . "::VERSION"}, $/; ---- 0.01 1.01

    The first goes and gets the thing in $x::VERSION (the $VERSION variable in the x:: namespace), then dereferences it as a symbolic reference in the main:: namespace. The second takes the $x variable in the main:: namespace and uses it to create a variable name that is used as a symbolic reference. In this case, the variable $Foo::VERSION.

    I'm not sure on the other questions, but you don't have a comma or semi-colon after the ${ ... }. Maybe that's changing your results?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: computed symbol references
by valentin (Abbot) on Jul 13, 2004 at 13:50 UTC
    Thank you all,
    1. I was confused by ${"$x::VERSION"} and ${"${x}::VERSION"} even if I know the difference, I could not see it.
    2. explained by 1.
    3. I missed the ;.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://373962]
Approved by Happy-the-monk
help
Chatterbox?
[GrandFather]: That's ok when the signal is a voltage, but there are three axis accelerometers, gyroscopes and magnetometers in these things! A little invention is needed at times!
[Corion]: GrandFather: Yeah, in that aspect, hardware is far more a black box than software
[GrandFather]: Oh, and the manufacturer tells lies too! I'm sure they don't mean to, but I know for sure some of the stuff they say is just wrong. It's possible they don't understand what we are asking, but that's not a good look either
[Corion]: GrandFather: Ah, unit testing hardware is fun, especially when the hardware is uncalibrated ...
[GrandFather]: Us: "What scaling do we need to apply to the numbers from the SDK for the wibble?". Them: "Oh, the numbers from the SDK for the wibble are already correct, they don't need scaling"
[GrandFather]: In our code: wibble range 1 scale by 1, range 2 scale by 2, range 3 scale by 4, range 4 scale by 8

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-08-24 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (365 votes). Check out past polls.

    Notices?