[NEWBIE] Expanding a perl variable twice inside a substitution

T

Tyson Marchuk

Sorry if this has been asked before but there are over 12k messages in this
group... none of my searches on the group turned up the answer to this.

Basically I was wondering how I can use a variable as the replacement in a
substituion.

ie. I want to do something like this:
$lineToBeReplaced =~ s/$searchString/$replaceString/;

Where $searchString and $replaceString are defined at run-time. The
$searchString seems to work fine but I can't seem to get the $replaceString
to work with backreferencing metacharacters. (eg. $1, $2 or \1, \2) Instead
if $replaceString is "$1" for example all the matches will be replaced with
a literal "$1" not the first matched section.

I know the search string is working properly because I've tried:
$lineToBeReplaced =~ s/$searchString/$1/;

And this works but then the replacement string isn't easily changeable.

I've tried a bunch of code I've found in other places including trying to
use (?{}) and eval but I can't seem to get this to work.


Thanks for any help you can provide,
-Tyson
 
B

Ben Morrow

Tyson Marchuk said:
Sorry if this has been asked before but there are over 12k messages in this
group... none of my searches on the group turned up the answer to this.

Basically I was wondering how I can use a variable as the replacement in a
substituion.

ie. I want to do something like this:
$lineToBeReplaced =~ s/$searchString/$replaceString/;

Where $searchString and $replaceString are defined at run-time. The
$searchString seems to work fine but I can't seem to get the $replaceString
to work with backreferencing metacharacters. (eg. $1, $2 or \1, \2) Instead
if $replaceString is "$1" for example all the matches will be replaced with
a literal "$1" not the first matched section.

I know the search string is working properly because I've tried:
$lineToBeReplaced =~ s/$searchString/$1/;

And this works but then the replacement string isn't easily changeable.

s/$searchString/qq!"$replaceString"!/ee;

Yeuuch. There must be a better way...

Ben
 
U

Uri Guttman

ask the OP why he wants this done. i bet there is an xy problem as this
is most likely a homegrown templating system with pure perl variables
and that is BAD. it means the templates know what perl vars must be
around and that limits the use of those vars for other purposes. it is
probably a security hole as well unless you trust the template
writers. on the whole it is a bad design choice and as such an XY problem.

f> How about just

f> s/$searchString/$replaceString/ee;

have you tried that? the /ee approach is known to only work with a
doubly double quoted string which yours doesn't have.

fifo: you have been trying to answer questions here which is good but
you have been wrong many times and obviously guess a lot. learn some
more perl before you guess wrong again.

uri
 
U

Uri Guttman

ask the OP why he wants this done. i bet there is an xy problem as this
is most likely a homegrown templating system with pure perl variables
and that is BAD. it means the templates know what perl vars must be
around and that limits the use of those vars for other purposes. it is
probably a security hole as well unless you trust the template
writers. on the whole it is a bad design choice and as such an XY problem.

f> How about just

f> s/$searchString/$replaceString/ee;

have you tried that? the /ee approach is known to only work with a
doubly double quoted string which yours doesn't have.

fifo: you have been trying to answer questions here which is good but
you have been wrong many times and obviously guess a lot. learn some
more perl before you guess wrong again.

uri
 
F

fifo

f> How about just

f> s/$searchString/$replaceString/ee;

have you tried that? the /ee approach is known to only work with a
doubly double quoted string which yours doesn't have.

You're right. I was assuming that $replaceString would be an
expression, but on reflection I think the OP probably wouldn't have
called the variable "replaceString" then. It does work with the OP's
example, which was $replaceString='$1'.
fifo: you have been trying to answer questions here which is good but
you have been wrong many times and obviously guess a lot. learn some
more perl before you guess wrong again.

I'll make sure to learn much more perl before my next wrong answer.
 
B

Brian McCauley

Yes, I think this is probably the single most frequently asked
question in these newsgroups not properly covered in the FAQ. It goes
in waves. Sometimes it is asked several times a week for several
weeks. Sometimes it goes unasked for months.

Actually this is in some ways just a special case of the FAQ "How can
I expand variables in text strings?" but INNSHO the answer given there
is seriously incomplete.

Also this special case is actually more common than any other from of
the question.

For many years I've been trying to get the FAQ fixed but the realy
simple soluition is considered "forbidden knowledge" that the FAQ
maintainers believe would taint your soul.

Since most theads on this sooner or latter degenerate into the old
flame-war about why the FAQ maintainers won't put the real answer in
the FAQ please search for phrase "How can I expand variables in text
strings?" and you'll find many threads on this exact question.
s/$searchString/qq!"$replaceString"!/ee;

Yeuuch. There must be a better way...

Yes there are many. See numerous previous threads on this subject.

E.g.

http://groups.google.com/[email protected]

(Note: see all three of my posts in that thread).

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top