demil
请教关于.操作符
#!/bin/perl -w
#use warnings;
use strict;
use Getopt::Long;
my @terms;
GetOptions('term:s'=>/@terms);
die "usage $0 [-t term [-t term] file .../n]" unless @terms;
my $regexp="";
print @terms;
foreach(@terms) {
$regexp .='print("$ARGV:$.('.$_.')$_/n") if //b'.$_.'/b/o;'; #其中的'.$_.'如何理解?
#print "$regexp/n";
}
print "searching with:/n$regexp";
my $loop='while(<>) {chomp; '.$regexp.'}'; #这里的'.$regexp.'如何离解呢?
eval $loop;
谢谢!
demil
不好意思,现在应该可以吧
#!/bin/perl -w
#use warnings;
use strict;
use Getopt::Long;
my @terms;
GetOptions('term:s'=>/@terms);
die "usage $0 [-t term [-t term] file .../n]" unless @terms;
my $regexp="";
print @terms;
foreach(@terms) {
$regexp .='print("$ARGV.('.$_.')$_/n" if //b'.$_.'/b/o;'; #其中的'.$_.'如何理解?
#print "$regexp/n";
}
print "searching with:/n$regexp";
my $loop='while(<> {chomp; '.$regexp.'}'; #这里的'.$regexp.'如何离解呢?
eval $loop;
谢谢!
churchmice
[quote]原帖由 [i]demil[/i] 于 2008-5-13 17:03 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8371626&ptid=1097258][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
这是书上的源码,没有错误,我把$regexp .='print("$ARGV.('.$_.')$_/n" if //b'.$_.'/b/o;'; 中的.去掉,即:
$regexp .='print("$ARGV($_)$_/n" if //b$_/b/o;';
会报错,这里为什么一定要用.呢?在Prin ... [/quote]
显然不一样么
'a'.$_'b'的话$_会被替换掉
你直接
'a$_b'的话$_不会被替换(interpolation)掉
什么时候用'',什么时候用"",什么时候变量会被解释,这个要注意