in reply to s/// in a sub : passing expressions
and we use it as followssub dyn_replace { my ($replace) = @_; my @groups; { no strict 'refs'; $groups[$_] = $$_ for 1 .. $#-; # the size of @- tells us the + number of capturing groups } $replace =~ s/\$(\d+)/$groups[$1]/g; return $replace; }
and it prints "x36y47", just as desired.my $pat = qr/foo(\d+)bar(\d+)/; my $rep = 'x$1y$2'; my $str = 'foo36bar47'; $str =~ s/$pat/dyn_replace($rep)/e; print "$str\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: s/// in a sub : passing expressions
by Tanktalus (Canon) on Oct 30, 2005 at 01:29 UTC | |
by Errto (Vicar) on Nov 01, 2005 at 01:41 UTC |