Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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;

returns:

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?

-Nich

Comment on Re-Interpolating a Scalar (a string)
Select or Download Code
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;

    -Colin.

    WHITEPAGES.COM | INC

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. :)

    -Nich
      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.
Re: Re-Interpolating a Scalar (a string)
by jeffa (Chancellor) 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;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-09-18 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls