Opening files with interpolation

M

Mat W

I need help opening files. Basically, I have a directory full of html
pages. These pages are templates, with variables where i want them. For
example, lets say i have a file called test.html in my pages directory, and
it contains the following:

<head><title>$mytitle</title></head>

Now, in the script there are times when i need to print this file. What i
want to do is first set the variables, then open the file and print the html
contents.

For example:
$mytitle = "TEST";
open (FILEIN, "./pages/test.html");
@contents = <FILEIN>;
close (FILEIN);
print @contents.

The above code opens files the files and reads them fine, but the problem is
the printing.
Instead of interpolating the contents of the file, it prints EXACTLY whats
in the file.
so, given whats above, the output is literally:

<head><title>$mytitle</title></head>

instead of being
<head><title>TEST</title></head>

Is there any way to open a file so that, when the contents are printed, perl
interprets any special characters? More or less what im trying to say is,
that when the file contents are printed, its like they are in single quotes
instead of double quotes (ie. variables arent processed).

Please help me! This is the only thing stopping me from completing my
project.
P.S. my html pages contain more than a variable for the title. They contain
variables in certain places so i can have list boxes and other things that
reflect the contents of a database.
 
G

Gunnar Hjalmarsson

Mat said:
I need help opening files.

The same question was posted to alt.perl and comp.lang.perl.misc as
well. Please post possible responses to those groups.

This groups is defunct.
 
C

Chris Lesner

Expand the variables using substitution like this:

perl -e'$p="this is the \$variable"; $p=~s{\$variable}{value}g; print
$p; '
 
S

Skeleton Man

I need help opening files. Basically, I have a directory full of html
pages. These pages are templates, with variables where i want them.For
example, lets say i have a file called test.html in my pages directory, and
it contains the following:

Now, in the script there are times when i need to print this file. What i
want to do is first set the variables, then open the file and print the html
contents.


Try this, it uses an associative array (hash) to store the template
variables:

#!/usr/bin/perl

# Replace $mytitle with "TEST TITLE" and $other with 'SOMETHING':
%vars = (mytitle => 'TEST TITLE', other => 'SOMETHING');

open (FILEIN, "html");
@contents = <FILEIN>;
close (FILEIN);

$content = join('',@contents);

foreach my $key (keys %vars){
$content =~ s/\$${key}/$vars{$key}/g;
}

print $content;


Regards,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top