Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: inserting HTML file in a PERL script

by thetallblondguy (Initiate)
on May 29, 2005 at 13:12 UTC ( [id://461510] : note . print w/replies, xml ) Need Help??


in reply to Re: inserting HTML file in a PERL script
in thread inserting HTML file in a PERL script

Thank you for your help everyone, it is highly appreciated.

 

About this script, it seems to be applicable only if the variable is named $variable

Could it be possible to give me the code for a script that would replace anything starting with $xxx ?

The variables that i have got in my html code can be named A-Z and '_' like $EMAIL or $CONTACT_ADDRESS

Replies are listed 'Best First'.
Re^3: inserting HTML file in a PERL script
by davidrw (Prior) on May 29, 2005 at 15:24 UTC
    yes, you could match on the $ and lookup the variable name in a hash, and replace with the value. Expects the template variables to be uppercase (A-Z and _ only) and the hash keys to be lower case.
    use strict; use warnings; my %data = ( first_name => 'Foo', email => 'Bar', ); $/ = undef; my $s = <DATA>; $s =~ s/\$([A-Z_]+)/exists $data{lc $1}?$data{lc $1}:$&/esg; print $s; __DATA__ First name is $FIRST_NAME and<br> email is: $EMAIL. junk is $JUNK
Re^3: inserting HTML file in a PERL script
by Tortue (Scribe) on May 29, 2005 at 19:04 UTC
    Just to generalize the script I gave you... This replaces any variable in the template with its value in the program. It's a quick-and-dirty fix that may solve your problem if you're in a rush. Of course, as others have noted, it would be better if you used a proper templating system. Note also that this only works for simple scalar variables (e.g. $thing, $ZOWIE), not for more complicated variables such as array or hash elements (e.g., $var[$num] or $var{thing}).
    my $template = snarf('template.htm'); while ($template =~ /(\$[:alpha:]\w*)/g) { my $variable_name = $1; my $value = eval $variable_name; $template =~ s/\Q$variable_name\E/$value/g; } print $template;