I like how Python can format a string with a dictionary:
print "%(key1)s and %(key2)s" % aDictObj
I want to achieve the same thing in Perl with a hash. Is there any fragment or small library?
EDIT:
Thanks for trying this answer. As for me, I came out with a short piece of code:
sub dict_replace { my ($tempStr, $tempHash) = @_; my $key; foreach $key (sort keys %$tempHash) { my $tmpTmp = $tempHash->{$key}; $tempStr =~ s/%\($key\)s/$tmpTmp/g; } return $tempStr; }
It just works. This is not as complete as formatting a Python string with a dictionary, but I would like to improve this.
source share