Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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.


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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://373962]
Approved by Happy-the-monk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2020-05-27 13:44 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (155 votes). Check out past polls.