I'm sorry, but I don't really recommend Zaxo's double e solution for serious purposes, as two e modifiers will have to evaluate the code on the right side every time a match is found and the replacement has to be calculated. A double e switch should usually be avoioded, just like a string eval. (If this isn't clear, s/$pat/$rep/ee is about the same as s/$pat/eval($rep)/e, and here the eval has to be run every time the pattern matches the string.)

A better solution would be to pass the replacement as a subroutine, so that the code doesn't have to be compiled multiple times. The first e modifier doesn't have to do this, as the code that generates the replacement string is always the same, so it doesn't have to be recompiled every time a match is found. (Of course, if the replace text is a constant string, you wouldn't need any e's.)

(This is of course for the case of your apply_regex_ee, where the replacement has to be computed. If the replacement is a constant string, as in apply_regex_e, you don't even need a single e, although it doesn't make the solution any worse to have the e switch there.)

Here's my version of the code. I'd like to thank chester for the clean example code.

use strict; use warnings; my $test_data = 'This is some test data: $888...!'; my $pattern = qr{(\$?[0-9,]+)}; my $replacement = '<test>$1<\/test>'; my $first_test = replace_with_constant($test_data, $pattern, $replacem +ent); my $replacement_func = sub { "<test>" . $1 . "<\/test>"; }; # OR my $replacement_func = sub { "<test>$1<\/test>"; }; my $second_test = replace_with_dynamic($test_data, $pattern, $replacem +ent_func); print $first_test, "\n", $second_test, "\n"; sub replace_with_constant { my ($data, $pat, $rep) = @_; $data =~ s/$pat/$rep/; # OR $data =~ s/$pat/$rep/e; return $data; } sub replace_with_dynamic { my ($data, $pat, $rep) = @_; $data =~ s/$pat/&$rep()/e; return $data; }

Update 2009 april 1: this seems to be a common question, Regular expression "replace string interpolation" problem asks it again.


In reply to Re^3: Regex as an argument to a sub by ambrus
in thread Regex as an argument to a sub by Shaune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.