in reply to qr and substitution
I want to pass an entire s/foo/bar/i regexp substitution into a method as a (preferrable single) parameter.No, I don't think so. In your place, I'd pass two things: a qr// regexp, and a code ref for the substitution part.Can that be done?
This makes use of the fact that $1 etc. are (localized) global variables.sub foo { my($qr, $replace) = @_; s/$qr/$replace->()/ge; } $_ = "This is a new dawn."; foo(qr/(\w+)/, sub { ucfirst $1 }); print;
Alternatively, pass just a code ref for the whole s/// statement, as a callback. This callback can do more than just a substitution, though.
sub bar { my $callback = shift; $callback->(); } bar(sub { s/(\w+)/\u$1/g; });
|
|---|