Hi.
Nobody else has mentioned this, but I think it's nice to have a name
to write to. '[EMAIL PROTECTED]' and 'rkl' don't do a lot for me!
<[EMAIL PROTECTED]> wrote:
>
> 1 - What is the best char to use in a file template?
> Also, please give me a good regex?
>
> I will have a formatted html page with some keys that I want to replace.
>
> For example,
>
> ...
> <!-- here my token is a ! -->
> <td>First Name:</td><td>!fname!</td>
> <td>Last Name:</td><td>~!lname!</td>
> ...
Unless there's a good reason not to, I would use an HTML comment tag
<!-- fname -->
<!-- lname -->
etc. so that the HTML parses OK before the subtitutions are done.
> 2 - Can I read in the whole page and do a replacement?
> I'm thinking it's either a line-by-line or a whole page as below:
>
> #line-by-line replace token
> $line ~= s/!fname!/$fname/
>
> #or put the whole template in
> $page ~= s/!fname!/$fname/
> $page ~= s/!lname!/$lname/
Your substitution is fine, just add a /g to it to change all tags
in the string instead of just the first. Like this
$line =~ s/<!-- fname -->/$fname/g;
Unless you have a good reason to read in the whole file at once,
process it a record at a time. Like this:
use strict;
use warnings;
open IN, 'template.htm' or die $!;
my %values = ( fname => 'Rob', lname => 'Dixon' );
while (<IN>) {
s/<!-- fname -->/$values{fname}/g;
s/<!-- lname -->/$values{lname}/g;
print;
}
close IN;
I hope that helps.
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]