Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Print substitution ?

Reply
Thread Tools

Print substitution ?

 
 
Bob
Guest
Posts: n/a
 
      09-30-2005
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,
 
Reply With Quote
 
 
 
 
michaelpgee@gmail.com
Guest
Posts: n/a
 
      09-30-2005
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);

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-30-2005

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

 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      09-30-2005
Bob <> 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
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      09-30-2005
Sherm Pendley <> writes:

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


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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-30-2005
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

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-30-2005
Paul Lalli wrote:

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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Should this substitution be compilable? valentin tihomirov VHDL 12 11-30-2004 03:44 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Converted to Mozilla but one thing missing - key macro/substitution no-spam Firefox 5 07-29-2003 08:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57