Replacing variable names with values via reference in regexp
- From: "Ceesaxp" <ceesaxp@xxxxxxxxx>
- Date: 2 Dec 2006 12:14:47 -0800
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
.
- Follow-Ups:
- Re: Replacing variable names with values via reference in regexp
- From: Uri Guttman
- Re: Replacing variable names with values via reference in regexp
- From: Gunnar Hjalmarsson
- Re: Replacing variable names with values via reference in regexp
- From: A. Sinan Unur
- Re: Replacing variable names with values via reference in regexp
- From: Mirco Wahab
- Re: Replacing variable names with values via reference in regexp
- Prev by Date: Re: regex question
- Next by Date: Re: If Perl support inline scripting...
- Previous by thread: FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org?
- Next by thread: Re: Replacing variable names with values via reference in regexp
- Index(es):
Relevant Pages
|