http://www.perlmonks.org?node_id=373962

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 ;.