in reply to Re: Curly braces variable
in thread Curly braces variable

How is that a symbolic reference, when the variable name is a literal in the source code?

Replies are listed 'Best First'.
Re^3: Curly braces variable
by tobyink (Canon) on Apr 30, 2012 at 21:16 UTC

    Same way this is...

    use strict 'refs'; ${ hello_world } = "hello world"; ${ $_ = hello_world } = "hello world";

    They're both basically the same construct, but the syntax on line #2 is a symbolic reference that use strict 'refs' treats as an exception, and allows. The "Not-so-symbolic references" section in perlref documents this.

    What it shows I suppose is that the line between symbolic references and hard references is slightly fuzzy.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      They're both basically the same construct...

      How do you figure? One of them is a literal identifier and the other is an expression.

      ... but the syntax on line #2 is a symbolic reference...

      Yes, because it's not a literal identifier. Compare:

      my ${      hello_world } = "hello world";

      ... to:

      my ${ $_ = hello_world } = "hello world";

      Perl complains about the latter because you can't use a scalar dereference as the name of a lexical variable in a declaration. (You don't get to use symbolic references with lexicals anyway, at least without XS.) A quick of the grammar allows you to use a block immediately following the scalar sigil, but you get the runtime error in the my op if you have anything other than a literal scalar name here.