in reply to Parsing a string from a file

Assuming the variables are declared, you can use eval ... BUT be very careful and be sure that you trust the data (i.e. that system() commands, etc can't be injected).

Basically, we want to accomplish this:
my @time = (1, 2, 3); my $counter = 5; my $s = "Today is $time[1] and you have seen this $counter times!"; print "$s";
So what we can do is this:
my @time = (1, 2, 3); my $counter = 5; my $s = 'Today is $time[1] and you have seen this $counter times!'; warn $s; warn eval "\"$s\""; __END__ Today is $time[1] and you have seen this $counter times! at blah.pl li +ne 4. Today is 2 and you have seen this 5 times! at blah.pl line 5.

Replies are listed 'Best First'.
Re^2: Parsing a string from a file
by RayQ (Initiate) on Jun 11, 2005 at 16:09 UTC
    Many thanks!
    *will remember eval for future use* :D
    And about the integrity of the data, I'm inputting it myself in these text files :D
    Again, thanks!
      Ah, so these are self-created data files? In that case (since you can format the data any way you want) you may want to consider using an existing template solution like Template Toolkit. This way you don't need to worry about eval or anything like that, plus you gets tons of other functionality.
      my @time = (1, 2, 3); my $counter = 5; my $data = { counter => $counter, time => \@time, }; my $s = 'Today is [% time.1 %] and you have seen this [% counter %] ti +me[% "s" IF counter > 1 %]!'; warn $s; use Template; my $template = Template->new(); my $out = ''; $template->process(\$s, $data, \$out) or die $template->error; warn $out; __END__ ~~~Output~~~: Today is [% time.1 %] and you have seen this [% counter %] time[% "s" +IF counter > 1 %]! at /tmp/t line 8. Today is 2 and you have seen this 5 times! at /tmp/t line 13.