Previously, I wanted to make my program more flexible, i.e. allow user to input the search string and replacement string freely in command line. For example, "replace.pl <matching strings> <replacement strings>", but when I was trying to achieving this, I found that the matching variables $1,$2,... can not work as I expected.

If the user input parameter for replacement string is something like "$abc$1 of 10", i.e. user only want the $1 can be replaced with matching value, but keep $abc as raw string, then it will not be so easy.

Chris's solution has enlightened me to come up new thoughts to solve the problem. I can try to do a trick that replace $1 to an actual value in my program.

I got the hint that /ee is required from the book "Perl Cookbook". (Chapter on expanding variables in user input) The quoting required some experimenting. I really do not fully understand the explanation (Summarized below)

The first \e works much like the eval {BLOCK} which you attempted. It is compiled along with the rest of the program. Subsequent \e's are like an eval "string".