What does +FOO mean?

F

fishfry

I'm looking at a lot of code where they have

use constant FOO => "foo";

and then later

my $hash{+FOO} ...

Can someone please explain what '+' signifies in this context?
 
J

John Bokma

fishfry said:
I'm looking at a lot of code where they have

use constant FOO => "foo";

and then later

my $hash{+FOO} ...

Can someone please explain what '+' signifies in this context?

$hash{FOO} is $hash{'FOO'}
$hash{+FOO} is $hash{"foo"}
 
T

Tassilo v. Parseval

Also sprach fishfry:
I'm looking at a lot of code where they have

use constant FOO => "foo";

and then later

my $hash{+FOO} ...

Can someone please explain what '+' signifies in this context?

Barewords in hash subscripts are treated by perl as strings. By writing
'{+FOO}' the subscript no longer qualifies as bareword and the Perl
interpreter then knows it is referring to an identifier, the constant
FOO.

Other ways to resolve this would be:

$hash{FOO()}
$hash{&FOO}

This works because under the hood constants are inlineable functions
(that is: functions with an empty prototype created at compile-time).

By the way:

my $hash{+FOO} ...

is a syntax-error. You cannot declare hash-elements with 'my'.

Tassilo
 
T

Tad McClellan

John Bokma said:
$hash{FOO} is $hash{'FOO'}
$hash{+FOO} is $hash{"foo"}


$hash{ FOO() } is $hash{"foo"} too.

(but the OP's "my $hash{+FOO}" isn't even Perl...)

The perl parser intuit()s that the FOO is a string, if you want it
to be a subroutine call instead (which is how "use constant"
implements constants), then you need to disambiguate it somehow
so that you get your desired semantic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top