in reply to Curly braces variable

This is a "symbolic reference", but happens to use a syntax that still passes strict.

Do you understand what happens here?

no strict qw(refs); my $variable = 'host_mail'; my ${ $variable } = '80.80.80.80';

How about this:

no strict qw(refs subs); my $variable = host_mail; my ${ $variable } = '80.80.80.80';

So it's not much of a stretch to get to this:

my ${ host_mail } = '80.80.80.80';

It's a little unusual to do. Usually strict disallows symbolic references, but in this case it allows it, because it's rarely harmful to do it. It's arguably poor style though, as it can result in confusion. (It confused you.)

The one place you do see it quite often though is in string interpolation:

my $scandal_topic = 'email'; say "We will call this scandal '${scandal_topic}gate'!"; # says "We will call this scandal 'emailgate'!";

That's because without the curlies we get:

my $scandal_topic = 'email'; say "We will call this scandal '$scandal_topicgate'!"; # dies "Global symbol "$scandal_topicgate" requires explicit package n +ame
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Curly braces variable
by chromatic (Archbishop) on Apr 30, 2012 at 18:27 UTC

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

      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.