Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interpreting Variables from external files 2

Status
Not open for further replies.

Rieekan

Programmer
Apr 2, 2001
737
US
I'm attempting to set up some template files (HTML for now) that will carry variables within them that I'd like to have read into a perl script and interpreted before sending to the browser. Does anyone here know of a function that will accomplish this task?

Thanks in advance!

- Rieekan
 
Is this what you want?
Code:
variablefile.pl:
$var = 8;
@array = ("A", "B", "C");
%hash = ( 32 => "Cherry", 43 => "Strawberry");

script using variable file:
require "variablefile.pl";
print "var=$var\n";
foreach $item (@array) {
    print "item=$item\n";
}
foreach $key (keys %hash) {
    print "key=$key value=$hash{$key}\n";
}
 
Not really. What I want is the ability to take a file like so:

Code:
text text text $var1 text text text
text text text $var2 text text text
(repeat for many lines)

and have a perl script read in the file, then interpret the variables and print to STDOUT. The only way I can think to do this is by substitution, but the files I'm reading could have as many as 200 variables to incorporate.

Does that make more sense?
 
Here's one way to do it.

InputFile:
Hello
The sky is $color.
Today is $day. I am $age years old.

Script:
Code:
$color = "blue";
$day = "Tuesday";
$age = 28;

$file = "InputFile";
open(F, $file) or die "Can't open $file: $!\n";
while(<F>) {
    chomp;
    $_ =~ s/\$(\w+)/$$1/g;
    print &quot;$_\n&quot;;
}
 
This will do what i think you are asking. It uses eval() to do variable interpolation like you want. You may have to stare at it for awhile. The file 'varinput' contains the two lines from your original post.

Code:
#!/usr/bin/perl -w

use strict;

my $var1 = &quot;my substitution&quot;;
my $var2 = &quot;another substitution&quot;;

open IN, &quot;varinput&quot; or die $!;
while (<IN>) {
    chomp;
    $_ = &quot;\$_ = \&quot;$_\&quot;&quot;;

    print &quot;$_\n&quot;;
    eval;
    print &quot;$_\n&quot;;
}

jaa
 
Oops! you should take out the first print() statement in the while loop. I just had it in there for debugging.

jaa
 
Thanks for your help guys, but I'm still running into one problem with your solution Justice. What I'm printing is an HTML file that has the variable names in various places in the file. When I use your solution, the page is printing out $_ = &quot;&quot; in many places (but not all) and I cannot figure out why. I know it has to do with the line:
Code:
$_ = &quot;\$_ = \&quot;$_\&quot;&quot;;

Below is the code used as well as the file I'm reading in.

File
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME=&quot;Generator&quot; CONTENT=&quot;EditPlus&quot;>
<META NAME=&quot;Author&quot; CONTENT=&quot;&quot;>
<META NAME=&quot;Keywords&quot; CONTENT=&quot;&quot;>
<META NAME=&quot;Description&quot; CONTENT=&quot;&quot;>
</HEAD>

<BODY>
<h2>This is my header</h2>
<p>The variable dsn has a value of: $dsn</p>
<p>The variable variable has a value of: $variable</p>
</BODY>
</HTML>

Script

Code:
#! /usr/bin/perl

use Strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);

my $var1 = &quot;Users&quot;;
my $var2 = &quot;Lots of text with spaces&quot;;

#*************************************************************

print header;

open (HTML, &quot;<test.html&quot;) or die &quot;Cannot open template: $!\n&quot;;
while (<HTML>)
{
    chomp;
    $_ = &quot;$_&quot;;

    eval;
    print &quot;$_\n&quot;;
}
 
Ah, the problem is the quotes in the html file. They need to be escaped before the line get evaled.

Code:
while (<HTML>)
{
    chomp;
    s/&quot;/\\&quot;/g;
    $_ = &quot;\$_= \&quot;$_\&quot; &quot;;
    eval;
    print &quot;$_\n&quot;;
}

jaa

 
Wonderful, it would be something simple that I missed. Thanks again for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top