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
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 {
Hallo all,
maybe this is not exactly a beginner question, but I could not find an
appropriate mailing list (all other lists seem to be developer realted).
Basically, I want to do the same as
$data =~ s/^foo (whatever) bar$/bar $1 baz/mg;
but with a different interface (because it has to
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