ew
Solomon"
Cc: "Josef Wolf" ;
Sent: Thursday, October 26, 2023 12:07 AM
Subject: RE: Using qr// with substitution and group-interpolation in the
substitution part
Josef,
Inspired by Levi's “eval” idea, here is my solution:
sub substitute_lines {
my ($content
things" command on your system.
-Original Message-
From: Claude Brown via beginners
Sent: Thursday, October 26, 2023 8:07 AM
To: Levi Elias Nystad-Johansen ; Andrew Solomon
Cc: Josef Wolf ; beginners@perl.org
Subject: RE: Using qr// with substitution and group-interpolation
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 = &substitute_lines($data, qr/^foo (whatever) bar$/m, 'bar $1 baz');
p
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 { $