我有以下 perl 子例程:
sub rep {
defined ($filein = shift) || die ("no filein");
defined ($fileout = shift) || die ("no fileout");
$look = shift;
$replace = shift;
open (infile, "$filein")|| die;
open (outfile, "> $fileout")|| die;
while (<infile>) {
s/$look/$replace/g;
print outfile;
}
(close the files)
}
和以下文字:
kuku(fred) foo(3)
kuku(barney) foo(198)
我想用以下结构调用它:
$look = kuku\((\w+)\) foo \((\d+)\),
$replace = gaga\(($1)\) bar\(($2)\).
但是当我使用以下内容(及其变体)调用 sub 时,我无法让它接受 $1、$2 格式:
&rep ($ARGV[0], $ARGV[1],
"kuku\\(\(\\w+\)\\) foo \\(\(\\d+\)\\)" ,
"gaga\\(\(\$1\)\\) bar\\(\(\$2\)\\)");
我得到的是:
gaga($1) bar($2)
gaga($1) bar($2)
我究竟做错了什么?如何使子程序将 $1\ $2 (...) 识别为搜索和替换的搜索结果?