in reply to Re: How do I pass $1 to s///?
in thread How do I pass $1 to s///?

No need to use eval, I'd prefer one of...
sub foo { (my $text = $_[0]) =~ s/$_[1]/$_[2]->($1)/e; print $text; } foo("This is test 1\n", qr/test (.*)/, sub{"fish $_[0]"});
...or...
sub foo { (my $text = $_[0]) =~ s/$_[1]/$_[2]->()/e; print $text; } foo("This is test 1\n", qr/test (.*)/, sub{"fish $1"});

Replies are listed 'Best First'.
Re^3: How do I pass $1 to s///?
by ikegami (Patriarch) on Jan 06, 2006 at 19:32 UTC
    Nice, as long as the parameters of foo aren't read a file.
      ~/perl/monks$ cat foo.txt sub{"fish $_[0]"};
      ...then...
      $foo_from_file = do "foo.txt"; foo("This is test 1\n", qr/test (.*)/, $foo_from_file); sub foo { (my $text = $_[0]) =~ s/$_[1]/$_[2]->($1)/e; print $text; }
        You're using eval (disguised as do), so my point stands.