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);