Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

printig with variables in text

by moked (Beadle)
on Aug 03, 2008 at 08:25 UTC ( [id://701868]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,
I wat to print the content of a file
while this file contains perl variables ($var)
I want to replace the parl variables with content found in the perl program.
Appreciate the help.

Thanks.
Moked

Replies are listed 'Best First'.
Re: printig with variables in text
by ysth (Canon) on Aug 03, 2008 at 09:42 UTC
    $ cat input.txt Put the $var1 in the $var2, please. $ cat filltemplate.pl use Template; use strict; use warnings; my $vars = { var1 => 'cookies', var2 => 'oven', }; my $tt = Template->new({INTERPOLATE => 1}); $tt->process('input.txt', $vars) || die $tt->error(); $ perl filltemplate.pl Put the cookies in the oven, please.
    Template does much more, of course.
Re: printig with variables in text
by pjotrik (Friar) on Aug 03, 2008 at 08:54 UTC
    eval can help you:
    #!/usr/bin/perl use warnings; use strict; my $variable = 'xxx'; my $another_one = 'aaa'; my $content = do { local $/; <DATA> }; print eval(qq("$content")); __DATA__ some text that may contain a $variable, or perhapse $another_one.
Re: printig with variables in text
by dHarry (Abbot) on Aug 03, 2008 at 10:35 UTC

    You could be a bit more specific, i.e. add some explanation on what exactly you are trying to achieve.

    As usual there are several approaches

    • One way to do it is to inline the file in a HEREDOC, the variables will be interpolated. (Maybe not the most elegant solution but I used it to good effect in QED solutions :-)
    • Another (better) option is to put certain tags (the variables) in the file, parse the file and replace the tags with meaningful values. You can create a temporary file containing the substituted values (let's say an "instantiation" of the template). I used this approach to generate XML files to request data from a system.
    • Or use Template as suggested above.

    Hope this helps

      One way to do it is to inline the file in a HEREDOC, the variables will be interpolated.
      I can't remember ever seeing this done before. Can you please give a quick code example or a link to some discussion of it?
        my $oo = 'oop'; my $oop = <<"POOP"; I like to say p$oo Hello $oo POOP print $oop __END__ I like to say poop Hello oop
Re: printig with variables in text
by shmem (Chancellor) on Aug 03, 2008 at 08:58 UTC

    What did you try? What is your input like, what is the desired output? See How (Not) To Ask A Question.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-06-18 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.