Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello I'm a perl newby, I found a strange variable declaration and I can't find any explanation of the syntax meaning.

 my ${host_mail}="80.80.80.80";

I don't now what is the meaning of the curly braces. Thank you very much.
Hello
P_Newby

Replies are listed 'Best First'.
Re: Curly braces variable
by toolic (Bishop) on Apr 30, 2012 at 13:58 UTC
    Refer to Scalar value constructors.

    B::Deparse shows how perl interprets this code:

    perl -MO=Deparse -e 'my ${host_mail}="80.80.80.80";' my $host_mail = '80.80.80.80'; -e syntax OK
    The curlies are allowed, but not required. I think is is more usual to use curlies only when referring to a variable, not when declaring it.
Re: Curly braces variable
by johngg (Canon) on Apr 30, 2012 at 14:46 UTC

    As toolic points out, the braces are usually employed when accessing a variable and are necessary when you have to interpolate a variable into a string and the variable name has to be disambiguated from following text. An alternative is to use concatenation. In the following code the braces are needed because there is no $preffish variable.

    knoppix@Microknoppix:~$ perl -E ' > @prefs = qw{ cat dog monk }; > foreach $pref ( @prefs ) > { > say qq{${pref}fish}; > say $pref . q{fish}; > }' catfish catfish dogfish dogfish monkfish monkfish knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Curly braces variable
by Anonymous Monk on Apr 30, 2012 at 13:57 UTC

    Apparently its valid syntax

    $ perl -MO=Deparse,-p -e " my ${host_mail} = qq[80.80.80.80]; (my $host_mail = '80.80.80.80'); -e syntax OK

    Though I'm sure many would consider it as bad as this nonsense

    $ perl -MO=Deparse,-p -e " my $ host_mail = qq[80. +80.80.80]; " (my $host_mail = '80.80.80.80'); -e syntax OK

    Don't write code like that, its obfuscated

Re: Curly braces variable
by tobyink (Canon) on Apr 30, 2012 at 16:55 UTC

    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'

      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'
Re: Curly braces variable
by Anonymous Monk on May 02, 2012 at 06:21 UTC

    Thanks very much everybody for your help and for the clarity of your answers. I think that now the point is completely clear.
    I can confirm that the variable is used for interpolation, maybe an overzealous approach took the writer of that code to use curlies in the declaration too.
    Thanks again
    Goodbye
    P_Newby