Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re-Interpolating a Scalar (a string)

by Nichodemus (Initiate)
on Jul 29, 2005 at 18:22 UTC ( #479500=perlquestion: print w/replies, xml ) Need Help??

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

I am relatively new to Perl -- I figure there must already be a way to do this without writing my own custom subroutine. Here's my question:

How can I force a scalar variable to be re-interpolated?

For example, lets say that this line of code:

print $raw;


fred has $color eyes

What I want to be able to do is later set $color to equal "blue." And then have:

$parsed = # ??? something to re-interpolate $raw

print $parsed;  # I get the text "fred has blue eyes"

Anyone have any ideas?


Replies are listed 'Best First'.
Re: Re-Interpolating a Scalar (a string)
by cmeyer (Pilgrim) on Jul 29, 2005 at 18:27 UTC

    perldoc -q "expand variables" says:

    How can I expand variables in text strings? Let's assume that you have a string like: $text = 'this has a $foo in it and a $bar'; If those were both global variables, then this would suffice: $text =~ s/\$(\w+)/${$1}/g; # no /e needed But since they are probably lexicals, or at least, they could b +e, you'd have to do this: $text =~ s/(\$\w+)/$1/eeg; die if $@; # needed /ee, not /e It's probably better in the general case to treat those variabl +es as entries in some special hash. For example: %user_defs = ( foo => 23, bar => 19, ); $text =~ s/\$(\w+)/$user_defs{$1}/g;



Re: Re-Interpolating a Scalar (a string)
by jeffa (Bishop) on Jul 29, 2005 at 18:49 UTC

    Use one of the various templating modules from CPAN:

    • Text::Template
      use strict; use warnings; use Text::Template; my $template = Text::Template->new( TYPE => 'STRING', SOURCE => 'fred has {$color} eyes' ); our $color = 'blue'; print $template->fill_in;
    • HTML::Template
      use strict; use warnings; use HTML::Template; my $data = 'fred has <tmpl_var color> eyes'; my $template = HTML::Template->new( scalarref => \$data, ); $template->param( color => 'brown', ); print $template->output;
    • Template
      use strict; use warnings; use Template; my $data = 'fred has [% color %] eyes'; my $vars = { color => 'blue', }; my $tt = Template->new; $tt->process(\$data, $vars) || die $tt->error;


    (the triplet paradiddle with high-hat)
Re: Re-Interpolating a Scalar (a string)
by Nichodemus (Initiate) on Jul 29, 2005 at 18:28 UTC
    Alright -- I am sorry to waste your time -- I have already figured it out!

    $raw = '$parsed = "fred has $color eyes";'; eval $raw; print $parsed;

    Sorry for the false alarm. :)

      While this will work fine, it's probably not a good idea. Especially if you don't know ahead of time what's going to be in $raw, any malicious code that gets put there will be evaled by your program. Further, string eval is quite slow from what I hear. Instead, I would highly reccomend using the hash lookup method provided by cmeyer above.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://479500]
Approved by kirbyk
Front-paged by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2022-12-08 19:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found