in reply to Drop in regex replacements?

One relatively simple way to accomplish this would be to use subrefs with the 'e' switch (though I'd still use a parser instead):

#!/usr/bin/perl -w use strict; my $foo = sub { "$1 years ago" }; my $bar = 'Four score and perhaps seven'; $bar =~ s/perhaps (seven)/&$foo/e; print $bar;

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: Drop in regex replacements?
by IOrdy (Friar) on Sep 09, 2002 at 16:41 UTC
    Thanks Ovid that makes more sense.

    My origional code was in php and that did support adding the replace part of the regex as a string from somewhere else (i.e. a hash). I was thinking if preg_replace() can do it why can't a real perl replace regex do it :-)