Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.36 How can I expand variables in text strings?

Reply
Thread Tools

FAQ 4.36 How can I expand variables in text strings?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      02-28-2011
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.36: How can I expand variables in text strings?

(contributed by brian d foy)

If you can avoid it, don't, or if you can use a templating system, such
as "Text::Template" or "Template" Toolkit, do that instead. You might
even be able to get the job done with "sprintf" or "printf":

my $string = sprintf 'Say hello to %s and %s', $foo, $bar;

However, for the one-off simple case where I don't want to pull out a
full templating system, I'll use a string that has two Perl scalar
variables in it. In this example, I want to expand $foo and $bar to
their variable's values:

my $foo = 'Fred';
my $bar = 'Barney';
$string = 'Say hello to $foo and $bar';

One way I can do this involves the substitution operator and a double
"/e" flag. The first "/e" evaluates $1 on the replacement side and turns
it into $foo. The second /e starts with $foo and replaces it with its
value. $foo, then, turns into 'Fred', and that's finally what's left in
the string:

$string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney'

The "/e" will also silently ignore violations of strict, replacing
undefined variable names with the empty string. Since I'm using the "/e"
flag (twice even!), I have all of the same security problems I have with
"eval" in its string form. If there's something odd in $foo, perhaps
something like "@{[ system "rm -rf /" ]}", then I could get myself in
trouble.

To get around the security problem, I could also pull the values from a
hash instead of evaluating variable names. Using a single "/e", I can
check the hash to ensure the value exists, and if it doesn't, I can
replace the missing value with a marker, in this case "???" to signal
that I missed something:

my $string = 'This has $foo and $bar';

my %Replacements = (
foo => 'Fred',
);

# $string =~ s/\$(\w+)/$Replacements{$1}/g;
$string =~ s/\$(\w+)/
exists $Replacements{$1} ? $Replacements{$1} : '???'
/eg;

print $string;



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 4.22 How do I expand function calls in a string? PerlFAQ Server Perl Misc 0 03-06-2011 11:00 PM
FAQ 4.25 How do I expand tabs in a string? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Expand/collapse treeview nodes by clicking the node text Tor Inge Rislaa ASP .Net 0 02-05-2007 10:31 PM



Advertisments