Or Leave Out the Quotes Entirely
A name that has no other interpretation in the grammar will be treated as if it were a quoted string. These are known as barewords.[50] As with filehandles and labels, a bareword that consists entirely of lowercase ASCII letters risks conflict with future reserved words. If you have warnings enabled, Perl will warn you about barewords. For example:
my @days = (Mon,Tue,Wed,Thu,Fri); print STDOUT hello, " ", world, "\n";
sets the array @days to the
short form of the weekdays and prints “hello world” followed by a newline on
STDOUT. If you leave the
filehandle out, Perl tries to interpret hello as a filehandle, resulting in a
syntax error. Because this is so error-prone, some people may wish
to avoid barewords entirely. The quoting operators listed earlier
provide many convenient forms, including the qw// “quote words” construct, which nicely
quotes a list of space-separated words:
my @days = qw(Mon Tue Wed Thu Fri); print STDOUT "hello world\n";
You can go as far as to outlaw barewords entirely. If you say:
use strict "subs";
then any bareword will produce a compile-time error. The restriction lasts through the end of the enclosing scope. An inner scope may countermand this by saying:
no strict "subs";
Outlawing barewords is such a good idea that if you say
use v5.12;
or higher, Perl turns on all strictures for you automatically.
Note that the bare identifiers in constructs like:
"${verb}able"
$days{Feb}are not considered barewords since they’re allowed by explicit ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access