in reply to Can I Use Variables In A Regex?

$l =~ s/\b$t\b/$b/eg;

The 'e' is required to treat the replacement as Perl code and not a literal string. You don't want the \b's in the replacement because the replacement isn't a regex - it's normally a literal string. You can chain 'e' so that the first evaluation would produce Perl code that would, itself, be evaluated.

This is all explained in the relevant page at http://perldoc.perl.org. (Scroll down a bit to find the s/// section.)


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: Can I Use Variables In A Regex?
by dave_the_m (Monsignor) on Oct 30, 2005 at 10:28 UTC
    The 'e' is required to treat the replacement as Perl code and not a literal string.
    Except that perl treats it as a double-quoted string which interpolates, so the /e isn't required in this case.

    Dave.