3 ms·
> For reasons I don't fully understand, barewords preceded by '-' are still allowed, even with "use strict". This - the fact, rather than any justification - i
by darrenf 5y ago
> For reasons I don't fully understand, barewords preceded by '-' are still allowed, even with "use strict".
This - the fact, rather than any justification - is documented in `perldoc perlop`:
Unary "-" performs arithmetic negation if the operand is numeric, including any string that looks like a number. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect of these rules is that -bareword is equivalent to the string "-bareword".
Typically Perlish, that means you can do stuff like the following because, I dunno, reasons?
$ perl -E ' use strict; use warnings; say - -foo' # or -(-foo)
+foo
> Also allowed as hash keys like $foo{bar}=1 or (foo => "bar").
Quoting `perldoc perlop` again:
The "=>" operator (sometimes pronounced "fat comma") is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes operands that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left operand can be quoted explicitly.