4 ms·
Most of this appears to be Perl's tolerance for "barewords" if you don't "use strict;". I suspect this dates back to the Perl4 -> Perl5 transition, when they r
by tyingq 5y ago
Most of this appears to be Perl's tolerance for "barewords" if you don't "use strict;".
I suspect this dates back to the Perl4 -> Perl5 transition, when they removed the requirement to call a subroutine using an ampersand, like &somesub could just be somesub.
Unlike, say Python, Perl doesn't require subroutines to be defined before they are called.
For reasons I don't fully understand, barewords preceded by '-' are still allowed, even with "use strict". Also allowed as hash keys like $foo{bar}=1 or (foo => "bar").
- nocman 5y ago> For reasons I don't fully understand, barewords preceded by '-' are still allowed, even with "use strict". I believe the primary motivation for this is so you can pass argument lists to subs in a key => value format without having to quote the key. For example: someSub( -opt1 => $opt1val, -opt2 => $opt2val ); [edited for formatting, which I never seem to get right the first time]
- gnubison 5y agoYou don't have to quote the LHS of the => operator, even if "use strict" is on. (That's the only way that it differs from a regular comma.) > 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. > Otherwise, the "=>" operator behaves exactly as the comma operator or list argument separator, according to context.
- 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.