[Nh-pm] quoting list keys & values

Erik Price erikprice at mac.com
Thu Aug 1 08:21:53 CDT 2002


I'm wondering the straight poop on when quotes are needed for strings in 
list values and hash keys.  In PHP, it's not enforced, but it's good 
form to quote your string literals so that they are not unintentionally 
interpolated if there is a string constant of the same name.  Is this 
also true of Perl?

%hash = (key_is_unquoted => 42);         # unquoted key name
print "$hash{key_is_unquoted}\n";        # unquoted key, prints: 42
print "$hash{'key_is_unquoted'}\n";      # quoted key, prints: 42

%hash2 = ('key_is_quoted' => 255);       # quoted key name
print "$hash2{'key_is_quoted}'\n";       # quoted key, prints: 255
print "$hash2{key_is_quoted}\n";         # unquoted key, prints: 255

%hash3 = (key => value);                 # unquoted key and value
print "$hash3{key}\n";                   # unquoted key, prints: value
print "$hash3{'key'}\n";                 # quoted key, prints: value

So (apparently), Perl and PHP are similar in that quoting hash keys is 
not enforced, nor is quoting list values that are string literals?  So 
then, that begs the logical question, is it considered good form to 
quote string literals in Perl (as in PHP) to avoid confusion with string 
constants, or is that a personal choice?

To add to the confusion, the qw() function turns whitespace-delimited 
unquoted string literals into a list of quoted string literals, if I'm 
not mistaken.

Your opinions and advice on this are appreciated.
Thanks,


Erik




More information about the Nh-pm mailing list