in reply to Variable name

... but it doesn't.

What does it do? (This is nearly always a more useful metric :)

I ask, because for me, (in the absence of strict), it works as expected whether I use globals or lexicals:

$foo = 'foo';; $bar = 'f';; print ${ $bar . 'oo' };; foo { my $foo = 'foo'; my $bar = 'f'; print ${ $bar . 'oo' }; } foo

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Variable name
by almut (Canon) on Dec 08, 2008 at 15:18 UTC
    (in the absence of strict), it works as expected whether I use globals or lexicals

    Really?  It doesn't work for me with lexicals (neither with Perl 5.8.8, nor 5.10.0) — which confirms what the docs are saying:

    "Only package variables (globals, even if localized) are visible to symbolic references. Lexical variables (declared with my()) aren't in a symbol table, and thus are invisible to this mechanism."
      Really? It doesn't work for me with lexicals

      My bad!

      The output I showed was genuine, but all conducted in a single run of the debugger.

      Which explains it, (though it doesn't exactly satisfy the concept of least surprise), as the globals also existed despite that I had created similarly named lexicals. Which, (apparently naively), I expected to override the globals at the local scope.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Which explains it, (though it doesn't exactly satisfy the concept of least surprise), as the globals also existed despite that I had created similarly named lexicals. Which, (apparently naively), I expected to override the globals at the local scope.
        I think that it's important to note that lexicals do override globals—more generally (and more precisely), that the variable declared later overrides the one declared earlier—unless you explicitly ask for globals. Of course, no one would expect $::foo to access a lexical. Similarly, since ${'foo'} requires us to look up a variable by name, rather than location, and since the only way to find variables by name is in the symbol table, we can't possibly 1 get a lexical this way.

        (I know that you know this, but I worry about casual future readers thinking that you're suggesting that globals always override lexicals.)

        1 As almut correctly points out below, “can't possibly” is almost never correct about Perl. I probably ought to have said “shouldn't”.

Re^2: Variable name
by nsteiner (Novice) on Dec 08, 2008 at 15:08 UTC
    Like I said:
    "Use of uninitialized value in print"
      Like I said: "Use of uninitialized value in print"

      Sorry, but where exactly did you say that?

      Hi all

      I am trying to concatenate a string and a value of a variable to construct the name of a known variable.

      For example:

      $foo="foo"; $bar="f"; print ${$bar.'oo'}

      should give out: "foo" but it doesn't.

      Now, before you say anything, yes, I know, I should use a hash instead, but humor me, would you ?

      Thanks

      Noam


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        In the OP's defense, it was in response to me after you replied to their original posting. Just a race condition and small bout of carelessness on the OP's part.

        --MidLifeXis