in reply to Re: Changing a single quoted string into an interpolated string
in thread Changing a single quoted string into an interpolated string
Update:
I completely fooled myself with a bug in my code. qq does not work with out doing an eval. busunsl's solution works as expected, but isn't optimal because of the taint issue and the fact that I'm actually using sprintf in my production to build a hash rather than using printf to dump the results to STDOUT. After playing with it a while, I came up with a solution that uses a translation table and a regex to convert the single quoted string to a double quoted string. Some sample code follows:
#!perl -w use strict; use Getopt::Std; use vars qw($opt_F); sub q2qq{ my $i = shift; print $i; my %t = map { $_, eval "qq(\\$_)" } split // , 'befnrt'; $i=~s/\\([befnrt])/$t{$1}/go; return $i; } getopts('F:'); print "Before: \n"; printf($opt_F, 'foo', 'bar'); $opt_F = q2qq($opt_F); print "\nAfter: \n\n"; printf ($opt_F, 'foo','bar');
The result of running sample.pl -F %s:\t%s\n yields:
----Before: foo:\tbar\n%s:\t%s\n After: foo: bar
|
|---|