For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2005 > Print substitution ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Print substitution ?
Bob

2005-09-29, 9:57 pm

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.

Thanks,
michaelpgee@gmail.com

2005-09-29, 9:57 pm

Try something more like:

my $name = 'Freddy';
open(my $header, '<', '../header.htm') or die "Can't read
'../header.htm': $!\n";
for (<$header> ) {
s/(\$\w+)/$1/eg;
print_html($_);
}
close($header);

Paul Lalli

2005-09-29, 9:57 pm


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.


Your Question is Asked Frequently
perldoc -q expand
How can I expand variables in text strings?

Paul Lalli

Sherm Pendley

2005-09-30, 3:56 am

Bob <uctraing@ultranet.com> writes:

> 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.


Search CPAN for the word "template" - this is very well-explored territory,
so there's little need to reinvent that particular wheel.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Sherm Pendley

2005-09-30, 3:56 am

Sherm Pendley <sherm@dot-app.org> writes:

> Bob <uctraing@ultranet.com> writes:
>
>
> Search CPAN for the word "template" - this is very well-explored territory,
> so there's little need to reinvent that particular wheel.


Forgot the link:

<http://search.cpan.org>

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
John Bokma

2005-09-30, 3:56 am

Bob <uctraing@ultranet.com> 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 :-)

Brian McCauley

2005-09-30, 6:58 pm

Paul Lalli wrote:

> Bob wrote:
>
>
> Your Question is Asked Frequently
> perldoc -q expand
> How can I expand variables in text strings?


But is _not_ well answered in the FAQ. For details please review
numerous previous threads containig the exact phrase "How can I expand
variables in text strings?"

For example.

http://groups.google.com/group/comp...8b9d342d1898a5d

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com