I need to pass a regex substitution as a variable:
sub proc { my $pattern = shift; my $txt = "foo baz"; $txt =~ $pattern; } my $pattern = 's/foo/bar/'; proc($pattern);
This, of course, doesn’t work. I tried eval’ing the substitution:
eval("$txt =~ $pattern;");
but that didn’t work either. What horribly obvious thing am I missing here?
Do you? Why not pass a code reference? Example:
In general, when you want to pass ‘something that does something’ to a subroutine (‘a regex substitution’ in the case of your question) the answer is to pass a reference to a piece of code. Higher Order Perl is a good book on the topic.