Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: inserting HTML file in a PERL script

by TedPride (Priest)
on May 29, 2005 at 18:32 UTC ( [id://461551] : note . print w/replies, xml ) Need Help??


in reply to inserting HTML file in a PERL script

davidrw's solution is the best imho, with some minor changes to the substitution line:
$s =~ s/\$([a-zA-Z]\w*)/exists $data{lc $1} ? $data{lc $1} : ''/esg;
There's no point allowing just uppercase letters and then lowercasing them. It's also not a good idea (at least in my opinion) to leave $vars that don't have a value defined as the original text. The intention was probably to keep things like $10.45 from being interpreted as '', but you can easily avoid this by defining your var names as starting with a letter.

Replies are listed 'Best First'.
Re^2: inserting HTML file in a PERL script
by davidrw (Prior) on May 29, 2005 at 19:18 UTC
    actually i hadn't thought of the $10.45 case, but that's a key point. The upper/lower casing I picked was pretty much just an arbitrary definition of this quick & dirty template system. My rationale was all upper for the template vars so that they stand out more in the template. And all lowercase for the keys since variable/key names are typically lowercase. Also, lc'ing it makes it tolerant of typos like $FIRSt_NAME in the template.

    As for what to do with unknown vars, i figured that leaving '$FOO' in there was better to make it obvious that it didn't match, whereas doing '' might not be noticed..

    But both ways are up to the template authoer on how they prefer it to work..