in reply to hash key

If $var contains double quotes, then your code will be accessing a hash key that also contains double quotes.

If you write $my_hash{"T::c"}, then Perl sees a double-quoted string as the hash key, but the value of the double quoted string is the string without the surrounding quotes. As an alternative, you can also see that it works the same with single quotes $my_hash{'T::c'} or alternative quotes $my_hash{qq(T::c)}.

Your variable $var is equal to '"T::c"' as you already write, but in Perl source code, (double) quotes do not show up in the values they surround.

Replies are listed 'Best First'.
Re^2: hash key
by thanos1983 (Parson) on May 30, 2017 at 11:49 UTC

    Hello aviw,

    I think this small tutorial contains good information quotes in Perl

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^2: hash key
by aviw (Acolyte) on May 30, 2017 at 11:40 UTC
    Thank you for the replay.
    yep the $my_hsdh{'T::c'} returned correct value.
    Your explanation is good yet I just don't understand what should I do since when added both ' or "
    $var = "'" . $var . "'";
    $var = "\"" . $var . "\"";
    didn't get value out of the $my_hash{$var}
    Could you kindly advise?

      When you write the following:

      $my_hash{"T::c"}

      Perl interprets the hash key as the string T::c. The quotes are only there so that Perl knows that you mean the string T::c and not something that Perl could interpret as something else (like a package name, in this case).

      Constructing a value that contains quotes will not make the lookup successful because the hash key as interpreted by Perl does not contain quotes.

      To look at what Perl sees in a hash, consider using Data::Dumper:

      use Data::Dumper; my %my_hash = ( '"with_quotes_in_key"' => 'a value', "without_quotes_in_key" => 'another value', bare_key => 'a third value', "T::c" => 'your value', '"Another::thing"' => 'a fifth value', ); print Dumper \%my_hash;
        TY, solved by $var = "$var".
        The issue is that I'm guessing & would like your assistance about it
        When the pm file with the hash was created using '::' it was printed with Data::Dumper by the 3rd party code.
        It automatically wraps '::' with '"'? to state that it's a string key not a package that should be evaluated - right?
        If not kindly explain the logic since I'm missing it.