[Perl] printing a hash

Richard Sevrinsky sevrins at mamash.com
Mon Jun 24 06:29:10 PDT 2002

Hashes are not interpolated inside double quotes. As a matter of fact, arrays were not always interpolated inside double quotes either. Bear in mind that your second "print" example is converting the hash to a list on the fly, which essentially become a list of parameters to the print statement. The language design tradeoff here is between forcing escaping of the percentage sign and coming up with a meaningful way of interpolating a hash. Not forcing us to escape the percent sign is much more useful:

print "$user_pct% of all perl users are cool dudes\n";

print $cgi->tr({ -width => "$column_width%"}, $cgi->td( "Cell data" ) );

That other design decision (interpolating arrays) forces us to do this:

print "Send all comments to $oncall_webmaster\@foo.org\n";

- Richie

>The following file:
>#!/usr/local/bin/perl -w
>use strict;
>my %arr_hash1 = ('1' , 'do' , '2' , 're' , '3' , 'me' ,
>		 '4' , 'fa' , '5' , 'so' , '6' , 'la' ,
>		 '7' , 'se' , '8' , 'do');
>print "%arr_hash1\n";
>print %arr_hash1, "\n";
>Gives when run:
>i.e. the second "print" worked as I expected, the first did not. How come?
>BTW, obviously this is not how I would REALLY print the contents of a hash,
>this is just for a quick look for debug purposes.
>10x in advance,
>Offer Kaye
>Perl mailing list
>Perl at perl.org.il

More information about the Perl mailing list