Re: Replacing variable names with values via reference in regexp
- From: "Brian McCauley" <nobull67@xxxxxxxxx>
- Date: 3 Dec 2006 08:37:24 -0800
Uri Guttman wrote:
"C" == Ceesaxp <ceesaxp@xxxxxxxxx> writes:
C> $var = "variable";
C> $a = 'This is a string with a <%=var%>!'; # single-quoted for no
C> variable substitution
C> $a =~ s/<%=([a-z]+)%>/{ $v=$1; $$v; }/eg;
C> print $a;
that is using eval string
No, it's using symrefs.
and that is very bad.
Well, at least not something to be done lightly.
There is a widely repeated misconception that the /e switch on s/// is
in some way related to eval(STRING). This is wrong. s///ee is related
to eval(STRING) but the single /e just causes the RHS to be treated
like a BLOCK. s///e does not involve runtime compilation any more than
grep {BLOCK} LIST involves runtime compilation.
Indeed I find it helps to think of s///e as the most primative
operation and think of s/// and s///ee in terms of being shorthands for
special cases of s///e.
s/FOO/BAR/e can be thought of as a weird sytax for
do_substitute(qr/FOO/,sub{BAR})
s/FOO/BAR/ is short for s/FOO/ "BAR" /e;
sFOO/BAR/ee is short for for s/FOO/ eval(BAR) /e;
.
- 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
- References:
- Replacing variable names with values via reference in regexp
- From: Ceesaxp
- Re: Replacing variable names with values via reference in regexp
- From: Uri Guttman
- Replacing variable names with values via reference in regexp
- Prev by Date: Re: how to do this with RE?
- Next by Date: Re: Bareword errors?
- Previous by thread: Re: Replacing variable names with values via reference in regexp
- Next by thread: Re: Replacing variable names with values via reference in regexp
- Index(es):