Hello,
I tried to find a solution that doesn't use eval, although if '$1' is sent
as a parameter to the subroutine as a simple string, I think it either need
to be eval'ed or replaced literally.
I made this solution to work with more capturing parans if necessary:
use strict;
use warnings;
I should add that if the script reads values of $regex or $subst from an
external source, then my use of eval is seriously flawed. For example, that
external source might provide this value for $subst:
/; system("do-bad-things"); qr/x/
The eval will execute the "do-bad-things" command
Josef,
Inspired by Levi's “eval” idea, here is my solution:
sub substitute_lines {
my ($contents, $regex, $subst) = @_;
eval "\$contents =~ s/$regex/$subst/g";
return $contents;
}
$data = "foo whatever bar";
$data = _lines($data, qr/^foo (whatever) bar$/m, 'bar $1 baz');
print
That's a fun question, Josef!
I don't think you can pass a replacement phrase around, so this is all I
came up with:
sub substitute_lines {
my ($contents, $subst) = @_;
$contents = $subst->($contents);
return $contents;
}
my $data = "foo whatever bar";
print(substitute_lines($data, sub {