When the pm file with the hash was created using '::' it was printed with Data::Dumper by the 3rd party code.

In your OP you were asking about a string '"T::c"'. I just want to ask to be sure: Are you in any way parsing this Data::Dumper output? If you are, then I think this should be addressed, because manually parsing Data::Dumper output is not a good idea. However, I am not sure if your question is maybe one of simply understanding the Data::Dumper output? In that case, let's say you have output like this:

$VAR1 = { foo => "bar", "T::c" => "quz" };

So if your question is, why is the hash key in the first case without quotes, i.e. my $var = 'foo'; $hash{$var}, and in the second case, why is the hash key not my $var = '"T::c"';, but it is my $var = 'T::c';? The answer to this question is here:

The => operator (sometimes pronounced "fat comma") is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes operands that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left operand can be quoted explicitly.

These rules for when the left-hand side of a fat comma is autoquoted are basically the same as the ones for hash subscripts, which I discussed at length here. In short, writing ( foo => "bar" ) is exactly the same as writing ( "foo" => "bar" ). However, ( T::c => "quz" ) is not the same as ( "T::c" => "quz" ), because T::c will not be autoquoted according to the above rules. Data::Dumper knows all of this, and, with the appropriate options set, will generate the output I show above, only quoting hash keys when it is necessary.

solved by $var = "$var"

Note that if $var is already a string, then $var="$var"; will not really do anything and can be left out.


In reply to Re^5: hash key by haukex
in thread hash key by aviw

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.