i have the following perl subroutine:
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)
}
and the following text:
kuku(fred) foo(3)
kuku(barney) foo(198)
我希望您能使用以下结构来调用它:
$look = kuku\((\w+)\) foo \((\d+)\),
$replace = gaga\(($1)\) bar\(($2)\).
但是当我使用以下方式(及其变体)调用子函数时,它不能接受 $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 (...)识别为搜索和替换的结果?