<p>I need to pass a regex substitution as a variable:</p>
<pre><code>sub proc {
    my $pattern = shift;
    my $txt = &quot;foo baz&quot;;

    $txt =~ $pattern;
}

my $pattern = 's/foo/bar/';
proc($pattern);
</code></pre>
<p>This, of course, doesn't work. I tried eval'ing the substitution:</p>
<pre><code>eval(&quot;$txt =~ $pattern;&quot;);
</code></pre>
<p>but that didn't work either. What horribly obvious thing am I missing here?</p>

Well, you can precompile the regular expression using the
qr//
operator.  But you can't pass an operator (s///).
```
```
$pattern = qr/foo/;

print "match!\n" if $text =~ $pattern;
```
```
But if you have to pass the substitution operator, you are down to passing either code or strings:
```
```
proc('$text =~ s/foo/bar');

sub proc {
   my $code = shift;

   ...

   eval $code;
}
```
```
or, code:
```
```
proc(sub {my $text = shift;  $text =~ s/foo/bar});

sub proc {
   my $code = shift;

   ...

   $code->("some text");
}
```
```