I posted my original question a bit too quickly. The qq operator does exactly what I want it to do without using an eval block. Is there any reason to use taint mode if the code isn't going to be evaluated? Also, I am checking the format of the argument for validity in my production code. I just cut it out to simplify the example code.

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
----
Coyote

In reply to Re: Re: Changing a single quoted string into an interpolated string by Coyote
in thread Changing a single quoted string into an interpolated string by Coyote

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.