C
Ceesaxp
Hi,
I am trying to make a small template-like expansion in my code, though
I could fetch variable values via a reference but looks like I am
stuck. A sample code like this:
$var = "variable";
$a = 'This is a string with a <%=var%>!'; # single-quoted for no
variable substitution
$a =~ s/<%=([a-z]+)%>/{ $v=$1; $$v; }/eg;
print $a;
works. However, when I put it into larger code like this:
#! /usr/bin/perl -w
use strict 'vars';
my $template1 = 'This is a string with a <%=var%>!';
my $template2 = 'This is a string with a <%=main::var%>!';
my $var = 'variable';
print tmplParse($template1)."\n";
print tmplParse($template2)."\n";
sub tmplParse {
my $tmpl = shift;
my $v;
$tmpl =~ s/<%=([a-z:0-9_]+)%>/{ $v=$1; warn "\$v is now '$v'\t \$\$v
is '$$v'\n"; $$v; }/eg;
return $tmpl;
}
All I keep on getting is
Use of uninitialized value in concatenation (.) or string at
template.pl line 13.
(which is where substitution should happen)
If my reading is correct, it is unable to find a reference to either
$var or $main::var. But even if I define a local $var and set its
value inside of tmplParse, I still get the same error.
Any ideas, pointers, suggestions?
Thanks,
Andrei
I am trying to make a small template-like expansion in my code, though
I could fetch variable values via a reference but looks like I am
stuck. A sample code like this:
$var = "variable";
$a = 'This is a string with a <%=var%>!'; # single-quoted for no
variable substitution
$a =~ s/<%=([a-z]+)%>/{ $v=$1; $$v; }/eg;
print $a;
works. However, when I put it into larger code like this:
#! /usr/bin/perl -w
use strict 'vars';
my $template1 = 'This is a string with a <%=var%>!';
my $template2 = 'This is a string with a <%=main::var%>!';
my $var = 'variable';
print tmplParse($template1)."\n";
print tmplParse($template2)."\n";
sub tmplParse {
my $tmpl = shift;
my $v;
$tmpl =~ s/<%=([a-z:0-9_]+)%>/{ $v=$1; warn "\$v is now '$v'\t \$\$v
is '$$v'\n"; $$v; }/eg;
return $tmpl;
}
All I keep on getting is
Use of uninitialized value in concatenation (.) or string at
template.pl line 13.
(which is where substitution should happen)
If my reading is correct, it is unable to find a reference to either
$var or $main::var. But even if I define a local $var and set its
value inside of tmplParse, I still get the same error.
Any ideas, pointers, suggestions?
Thanks,
Andrei