CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
another simple question



Jul 23, 2001, 12:43 AM

Post #1 of 4 (1100 views)
another simple question Can't Post

hello everyone say i have a script

$r="i am $teststring";
print "$r";

when i run it i get "i am tester" as output. this is easy. but if i try to get $r from the file with

open (TEMPLATE,"test.txt");
while (<TEMPLATE>) {

(where text.txt contains only one string "i am $teststring") i get a "i am $teststring" as output.

this is a silly question, i know, so please forgive me , but how do i overcome this? i'm stuck :(

Enthusiast / Moderator

Jul 23, 2001, 1:12 AM

Post #2 of 4 (1099 views)
Re: another simple question [In reply to] Can't Post


the reason why this doesn't work is simple. The problem is that double quotes only interpolate once. That means, $_ is interpolated, and any variable name contained in $_ is not. Otherwise, strange things could go on. (Imagine you would read the string $_ from the file, you would end up in an infinite recursion.)
Anyway, to do what you want you have to evaluate the string you just read using eval:

while (<TEMPLATE>) { 
$r = eval qq("$_") or die "$@";

The 'or die ...' is optional, but I strongly recommend it when using eval. Of course, you can also implement your own error checking mechanism that doesn't die.
However, I personally don't like the idea of reading variable names from files. Sure, since this is Perl, you can do nearly everything, but should you? Try to think of a solution that's not so tricky at all, and you can even get rid of the eval.
If you have problems to figure out an appropriate solution, here's the place to ask.

-- Marcus



Jul 23, 2001, 1:27 AM

Post #3 of 4 (1095 views)
Re: another simple question [In reply to] Can't Post

thanx marcus! your worked fine for me. a little bit about my task. it is to read some html template file into an array, insert some strings here and there into it and then return the parsed thing to the browser. before i used to insert <! -- variable_to_change --> into a template and do a =~s/// thing on the array, containing the template, but as number of variables grew my script became too big and complicated, though i was using hashes to store var=value pairs. and that is why i wanted to put 'em all into template itself.

and THANX once again for the answer.


Jul 26, 2001, 2:13 PM

Post #4 of 4 (1070 views)
Re: another simple question [In reply to] Can't Post

I understand what you were saying about double-interpolation and how when stringified with double-quotes, variables are interpolated only once. However, I tried the same thing that jd did but did not enclose the variable in double quotes:

'test.txt' has only '$name' in it on a line by itself with no newline.

open FH, "test.txt" or die $!; 

$name = "Dan";
while (<FH>) {
$r = $_;
print $r, "\n";

I tried print '$_' as well but the result is the same. Does the Perl interpreter wrap double quotes around any arguments to the print function?

dan Wink


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives