Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Substituting Variables while reading text file

by mohanm_eee (Initiate)
on Aug 05, 2014 at 17:48 UTC ( #1096329=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I have a text file as below and saved it as "file.txt"

"create variable $VAR alarm_object 0"

Iam trying to read this in a perl script and printing it by substituting the "$VAR" variable

$VAR = TEMP_VAR open (FILE,"<", "file.txt") or die $!; while (<FILE>) { chomp; eval(print "$_\n"); } close (FILE);

I want the output as

"create variable TEMP_VAR alarm_object 0"

I tried to achieve this using eval function. It is not working as expected.

How can i achieve this in perl?

Replies are listed 'Best First'.
Re: Substituting Variables while reading text file
by toolic (Bishop) on Aug 05, 2014 at 18:04 UTC
    s///
    use warnings; use strict; my $VAR = 'TEMP_VAR'; while (<DATA>) { s/\$VAR\b/$VAR/g; print; } __DATA__ create variable $VAR alarm_object 0
Re: Substituting Variables while reading text file
by frozenwithjoy (Priest) on Aug 05, 2014 at 18:05 UTC

    Edit: Oops, toolic beat me to the punch!

    What about something like this:
    #!/usr/bin/env perl use strict; use warnings; my $VAR = "TEMP_VAR"; while (<DATA>) { s/\$VAR/$VAR/; print; } __DATA__ create variable $VAR alarm_object 0
Re: Substituting Variables while reading text file
by Laurent_R (Canon) on Aug 05, 2014 at 18:52 UTC
    The problem is probably that you are using a bare word instead of a string. Try changing:
    $VAR = TEMP_VAR
    to
    my $VAR = "TEMP_VAR";
    and it should work. My quick test with a Perl one-liner under Unix:
    $ echo 'create variable $VAR alarm_object 0' | perl -e '$VAR = "TEMP_V +AR"; while (<>) { chomp; eval (qq{print "$_\n";});}' create variable TEMP_VAR alarm_object 0
    My understanding is that this is closer to what the OP wanted than s///, but I may of course be wrong.
Re: Substituting Variables while reading text file
by aitap (Curate) on Aug 07, 2014 at 07:21 UTC

    String::Interpolate might be useful in this task. If you move your variables to be interpolated to a hash, you will be able to do it yourself, like this:

    my %variables = ( ... ); ...; while (<FILE>) { s/\$(\w+)/$variables{$1}/eg; print; }
    (untested).

    What you are writing looks like a templating engine. Unless it's a "homework" question, it might be useful to look at Text::Template, Mason and Template.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-06-06 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (26 votes). Check out past polls.

    Notices?