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

~/perl/monks$ cat foo.txt fish $1 ~/perl/monks$
...then...
#!/usr/bin/perl open FOO, "<foo.txt"; $foo_from_file = <FOO>; foo("This is test 1\n", qr/test (.*)/, $foo_from_file); sub foo { (my $text = $_[0]) =~ s/$_[1]/interpolate($_[2],$1)/e; print $text; } sub interpolate { my ($str,@vars) = @_; $str =~ s/\$(\d+)/$vars[$1-1]/g; $str }

Replies are listed 'Best First'.
Re^7: How do I pass $1 to s///?
by ikegami (Patriarch) on Jan 06, 2006 at 21:10 UTC
    You've just written your own template system. Why not use an existing one? There's even one that interpolates as Perl would. Like most homegrown template systems, yours has a bug. It doesn't provide a means of outputing "$1".