[Nh-pm] quoting list keys & values

Kevin D. Clark kclark at CetaceanNetworks.com
Fri Aug 2 08:29:44 CDT 2002


Erik Price <erikprice at mac.com> writes:

> 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?

Yes, this is considered good form in Perl too.  In fact, if your turn
warnings on in Perl (via "-w"), the Perl compiler will complain.  This
is a Good Thing.

An unadorned identifier in a Perl program that has no other meaning is
considered to be a "bareword", and barewords get interpreted as
strings.

> 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?

...but if you have a Perl program that contains a bareword, and if some
later version of Perl decides to use this same identifier for some
purpose, then your program will likely act strangely.

So it is considered to be a best practice to quote your strings.

> 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.

No confusion here.  This is what qw() is supposed to do.

> Your opinions and advice on this are appreciated.

Use "-w".  Use strict.  This is my advice.

Regards,

--kevin
-- 
Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA)
cetaceannetworks.com!kclark (GnuPG ID: B280F24E)
alumni.unh.edu!kdc




More information about the Nh-pm mailing list