Tuesday, March 1, 2011

How do I interpolate a Perl hash element in a string?

How do I print a hash from within a string, as in

print "\n[*] Query : $select { query }";

versus

print "\n[*] Query : " . $select { query };
From stackoverflow
  • You may need to eliminate the extra spaces:

    print "\n[*] Query : $select{query}";
    

    With a space after $select, Perl thinks that you are done with the interpolation of that variable, and treats the following text (including the curly braces) literally.

    PoorLuzer : What if the key was yet again a variable : print "\n[*] Query : $select{$query}"; ?
    Greg Hewgill : That should work fine too (as long as you don't put any extra spaces in the interpolation).

0 comments:

Post a Comment