Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Changing a single quoted string into an interpolated string

by Coyote (Deacon)
on Aug 28, 2001 at 00:31 UTC ( [id://108256]=perlquestion: print w/replies, xml ) Need Help??

Coyote has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a script that takes a printf style format string as a parameter (using Getopt::Std) to be used later in the program. Here's a simplified version of my code:
#!/usr/bin/perl -w use strict; use Getopt::Std; use vars qw($opt_F); getopts('F:'); printf($opt_F, 'foo', 'bar');
When I run the command foo.pl -F %s:\t%s\n, I get foo:\tbar\n. printf is treating $opt_F as a single quoted string. Is there anyway I can coerce the single quoted string into a double quotish string that will properly interpolate the \t and \n characters?

----
Coyote

Replies are listed 'Best First'.
Re: Changing a single quoted string into an interpolated string
by busunsl (Vicar) on Aug 28, 2001 at 00:36 UTC
    One way is eval:

    eval qq{printf("$opt_F", 'foo', 'bar')};

    But evaling something that is given on the commandline isn't probably a good idea!
    Use taint mode and untaint it first!

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://108256]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found