This is certainly an 'Advanced' trick. It is probably best to quote everything until you know what can go without, as there are some surprising things that can crop up once in a while, especially if you're brave enough to use Perl without '-w' or 'strict'.

As an introduction, though, there are a number of contexts where Perl is smart enough to recognize 'strings' even though they are not explicitly inside of quotes. This holds true even under '-w' and 'strict', which are extremely bitter about so-called bare-words, or ambiguous strings, and they will fiercely protest if given an opportunity.

A "safe string" is composed of one or more alphanumeric characters, with underscore also available, but the first character has to be a letter or a dash. You could define this as a regular expression: /\-?[A-Za-z_][A-Za-z0-9_]*/.

So, here are some examples:
Safe Unsafe ------------------------------------------ foo foo bar -foo --foo foo_bar foo+bar jane3 3jane
If you get too ambitious, Perl might think you're making a subroutine reference (i.e. &foo), or are trying to reference an internal function (i.e. delete) In a hash context, for example, Perl "knows" what you mean when you leave a bare 'safe string'. You might do this inside a hash definition:
my (%hash) = ( key_1 => 'value1', key_2 => 'value_2' ); print $cgi->textfield(-name => 'input1');
Or, inside a hash reference:      $hash{key_3} = 'value3'; The same sort of trick applies to where quotation is required in HTML, as you can often get away with no quotes at all if you stick to letters and numbers only.

If you have a syntax-highlighting editor, you can actually see the parser change highlighting when you type '=>' after a "safe string", as it now realizes what you mean.

In reply to Re: using quotes in hash keys by tadman
in thread using quotes in hash keys by thefid

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.