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


In reply to quoting style for lists by erikprice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.