in reply to lost in eval and regexp
You should really really really really really really use an existing template module. You would have installed and learned it in the time it took you to get this question answered. Converting your templates over isn't all that hard either.
sub convert { my ($template_name) = @_; my $template; { open my $fh, '<', $template_name or die("Unable to open template file \"$template_name\": $!\ +n"); local $/; $template = <$fh>; } $template =~ s/~~\$(.+?)~~/... new syntax .../g; { open my $fh, '>', $template_name or die("Unable to overwrite template file \"$template_name\" +: $!\n"); print $fh $template; } }
At the very least, pass your variables in a hash. That will get rid of the eval EXPR and solve all scoping problems.
sub replace { my ($template_name, $params) = @_; my $template; { open my $fh, '<', $template_name or die("Unable to open template file \"$template_name\": $!\ +n"); local $/; $template = <$fh>; } $template =~ s/~~\$(.+?)~~/ if (exists($params->{$1})) { if (defined($params->{$1})) { $params->{$1} } else { warn("Template file \"$template_name\" references undefine +d variable $params->{$1}\n"); '' } } else { warn("Template file \"$template_name\" references non-existin +g variable $params->{$1}\n"); '' } /eg; print("$template\n"); } my %params = ( cell => ..., day_prev_month => ..., ); replace($template_name, \%params);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: lost in eval and regexp
by lepetitalbert (Abbot) on Nov 21, 2007 at 16:06 UTC | |
by ikegami (Patriarch) on Nov 21, 2007 at 18:20 UTC | |
by lepetitalbert (Abbot) on Nov 21, 2007 at 17:02 UTC |