Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: hash key

by haukex (Archbishop)
on May 31, 2017 at 13:49 UTC ( [id://1191717]=note: print w/replies, xml ) Need Help??


in reply to Re^4: hash key
in thread hash key

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1191717]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-20 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found