Re: Uer Perl Variable in file
by choroba (Cardinal) on Aug 06, 2012 at 13:57 UTC
|
Using a hash instead of variables is safer (no eval needed). Something like this:
#!/usr/bin/perl
use warnings;
use strict;
my %replace = (name => 'Joe',
fun => 'a game');
while (<DATA>) {
for my $key (keys %replace) {
s/\$$key/$replace{$key}/g;
}
print;
}
__DATA__
Hullo $name,
are you ready for $fun?
| [reply] [d/l] |
|
#!/usr/bin/perl
use warnings;
use strict;
my %replace = (name => 'Joe',
fun => 'a game');
my $search_for = join '|', map quotemeta, keys %replace;
while (<DATA>) {
s/\$($search_for)/$replace{$1}/g;
print;
}
I avoid nested loops where possible.
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
Re: Uer Perl Variable in txt file
by rovf (Priest) on Aug 06, 2012 at 14:52 UTC
|
Although choroba already warned against using eval - and he is certainly right with it -, you can consider an eval-solution, if you understand the risks (and, in particular, if you have control over what goes into your file).
In this case, you could do:
use strict; use warnings;
while(my $line=<FH>) {
print(eval "qq($line)");
}
This solution is much simpler than doing a substitution manually, but never forget: Whoever creates file.txt, can get *any* code being executed by your program; it requires that every variable referenced in the file, exists in the scope of evaluation (and will be replaced); and finally, that, if file.txt contains parentheses, that they are balanced.
--
Ronald Fischer <ynnor@mm.st>
| [reply] [d/l] |
|
Thank you very much. It solved my problem. I can use eval without worry as i am the one who is creating the file.
-Vinay
| [reply] |
|
Thank you very much. It solved my problem. I can use eval without worry as i am the one who is creating the file. -Vinay And you never make mistakes? :) I would worry about making mistakes, maybe not today, but a week from now, so I would avoid string eval unless this was a single-use-program, only to be used once
Otherwise I would use String::Interpolate or String::Interpolate::RE or another similar abstraction instead
| [reply] |
|
eval stinks in such an application, as of course well you know. It would only "work perfectly" in a perfect situation, and who knows what it would do at all other times. Unacceptable.
| [reply] |
|
| [reply] [d/l] |
Re: Uer Perl Variable in txt file
by linuxkid (Sexton) on Aug 06, 2012 at 15:41 UTC
|
Have you considered Template, the template toolkit?
--linuxkid
imrunningoutofideas.co.cc
| [reply] [d/l] |
Re: Uer Perl Variable in file
by Anonymous Monk on Aug 06, 2012 at 14:24 UTC
|
I don't feel that I understand what you are getting, based on the snippet of source code that I see ... but here is one important thing to keep in mind: the difference between single and double quotes.
A string such as 'This is $name' will be output literally as it appears, whereas a string in double quotes such as "This is $name" will have the variable-name interpolated. That is, if $name is Fred, you get: This is Fred in the second case but not the first.
| [reply] |
|
I have a file which has contents like this
______file.txt_______
I am $name
I am from $city
I live $food
I got $car
Using perl code I am reading line one by one from file.txt. When I print line using $_ it does not replace variable in text file with variable values in .pl file but it prints the line as it is.
-Vinay
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |