in reply to using functions within regex

I Tried to use a function in a replace regex

The second half of the substitution operator (s///) is just a string, not a regex.

One way to do it is:

$t =~ s/$somePattern/@{[ some_function($1) ]}/g;

Replies are listed 'Best First'.
Re^2: using functions within regex
by davido (Cardinal) on May 04, 2011 at 05:45 UTC

    That would be the perfect way, if the /e modifier wasn't specifically designed to provide a means of executing code on the RHS of the s/// operator, without jumping through that hoop.


    Dave

      Not so perfect, in any event. Here's a snippet of a debugging session

      DB<1> $t = 'teach an old dog new tricks' DB<2> $t =~ s/(dog)/@{[ reverse($1) ]}/g DB<3> x $t 0 'teach an old dog new tricks'
      You'd have to do
      $t =~ s/(dog)/@{[ scalar(reverse($1)) ]}/g