in reply to Why is +CONST left to a fat comma not treated as in $hash{+CONST}?
i would like to understand why the '+' notation does not work as expected
unary-+ literally has no effect. It does nothing. It just returns the value provided to it.
It's even transparent to context
$ perl -Mv5.14 -e'my @a = qw( d e f ); say for +@a' d e f
So why do we use it?
Well, the Perl grammar has ambiguities. The same sequence of tokens could have two valid meanings. So Perl sometimes has to guess which meaning you mean. By inserting the do-nothing unary-+ in that sequence, we can sometimes create a sequence that's no longer ambiguous, or one that changes how Perl chooses to resolve the ambiguity.
For example, the { in map { could be the start of a block or it could be the start of a hash constructor. This is an ambiguity. However, the { in map +{ can only be the start of a hash constructor. There is no ambiguity.
So what about $hash{ +FOO }?
$hash{ FOO } is ambiguous, except it's documented that Perl will auto-quote what's in the curlies if it's entirely an identifier. Therefore, this is treated as $hash{ "FOO" }.
$hash{ +FOO } avoids that. Since the entirety of the contents of the curlies is no longer entirely an identifier, it's not auto-quoted, letting FOO have the same meaning it would have in my $key = FOO;.
So what about +FOO => ...?
=> auto-quotes the identifier that precedes it, if any. With or without the +, FOO is still right before the => and thus gets treated as "FOO".
|
|---|