[Nh-pm] quoting list keys & values
Erik Price
erikprice at mac.com
Fri Aug 2 09:16:14 CDT 2002
On Friday, August 2, 2002, at 09:51 AM, Ben Boulanger wrote:
> It -is- considered good form to quote strings in Perl. In fact, all of
> the perl scripts I write begin with perl -w and use strict - strict subs
> disallows unknown barewords (AFAIK) completely, where -w (warnings on)
> warns you about them. For example:
>
> [bboulanger at .. bboulanger]$ perl -e 'use strict; my $junk = blah;'
> Bareword "blah" not allowed while "strict subs" in use at -e line 1.
> Execution of -e aborted due to compilation errors.
>
> [bboulanger at .. bboulanger]$ perl -we 'my $junk = blah;'
> Unquoted string "blah" may clash with future reserved word at -e line 1.
Word to that. (And I'll definitely take you up on the advice of using
strict and -w.) But my confusion arose from the fact that even when
using strict, hash keys are exempt from the requirement of being quoted
strings.
localhost:~/tmp$ perl -we 'use strict; my %hash = ('a' => 1); print
"$hash{a}\n";'
1
Not only that, but when constructing a list for assignment to a hash,
the string literal to the left of a fat arrow (=>) doesn't need to be
quoted either!
localhost:~/tmp$ perl -we 'use strict; my %hash = (a => 1); print
"$hash{a}\n";'
1
This is what I was wondering about. You're not forced to quote these
particular string literals even with -w and strict. But ... is it good
form to do so?
localhost:~/tmp$ perl -we 'use strict; my %hash = ('a' => 1); print
"$hash{'a'}\n";'
1
Erik
---
Erik Price
email: erikprice at mac.com
jabber: erikprice at jabber.org
More information about the Nh-pm
mailing list