Re: Using qr// with substitution and group-interpolation in the substitution part

2023-10-26 Thread Octavian Rasnita
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;

RE: Using qr// with substitution and group-interpolation in the substitution part

2023-10-25 Thread Claude Brown via beginners
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

RE: Using qr// with substitution and group-interpolation in the substitution part

2023-10-25 Thread Claude Brown via beginners
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

Re: Using qr// with substitution and group-interpolation in the substitution part

2023-10-25 Thread Andrew Solomon
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 {