考虑这个简单的程序。您能解释一下为什么在取消注释前两行后输出会有所不同吗?我的哈希发生了什么use strict
?如何修复程序以供使用use strict
?
echo -e "key1\nkey2\nkey3" | perl -lne '
#use strict; use warnings;
#my %hash;
BEGIN {
$hash{'key3'} = "value";
}
chomp;
if ($hash{$_}) {
print "$_ matched";
} else {
print "$_ unmatched ";
}
'
Output:
key1 unmatched
key2 unmatched
key3 matched
perl -ln
包装代码,这样你最终会得到
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (<>) {
chomp;
use strict; use warnings;
my %hash;
BEGIN { $hash{key3} = "value"; }
...
}
注意你如何创建一个新的%hash
每行?如果你想使用use strict;
,您将使用包变量。
perl -lne'
use strict; use warnings;
our %hash;
BEGIN { $hash{key3} = "value"; }
...
'
否则,你必须放弃-n
.
perl -le'
use strict; use warnings;
my %hash = ( key3 => "value" );
while (<>) {
chomp;
...
}
'
PS - 正如您可能已经注意到的那样,这是没有用的chomp
当同时使用时-l
and -n
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)