Bob <> wrote:
> I am trying to read in the contents of a file (which contains psuedo
> variables) and then print the file's contents and have variable
> substitution occur.
>
> Example:
>
> [file contains this text: "Your name is $name"]
>
> open (HEADER, "< ../header.htm")
> my @headerLines = <HEADER>;
> my $name = "Freddy";
> print_html("@headerLines");
>
> When I do this, I get the contents of the file, but the variable does
> not get substituted. So the output looks like this:
>
> Your name is $name
>
> Suggestions ? This is actually part of a much larger file read with
> many more variables and I could really use substitution.
my %vars = (
name => "Freddy",
:
:
);
while ( my $line = <HEADER> )
$line =~ s{\$(\w+)}{
defined $vars{ $1 } ? $vars{ $1 } : 'ERROR'
}ge;
print $line;
}
(untested, use at your own risk, etc.)
But as Sherm already explained: there are many template solutions @
CPAN.
Some tips though:
check your open
don't use camelCase (it's Perl, not Java)
"@headerLines" probably doesn't do what you want.
--
John Small Perl scripts:
http://johnbokma.com/perl/
Perl programmer available:
http://castleamber.com/
I ploink googlegroups.com