unless(-e file){
print"file not exists!\n" ;
} 使用or操作符
and和or都是短路操作符,a and b如果a为false,则不再计算b,a or b若a为真则不再计算b。所以用a or b可以实现和unless类似的功能,其语义是,除非满足条件a,否则执行b。看一个例子
chdir'/usr/spool/news' or die"Can't cd to spool: $!\n" 一次性读入整个文件
方法一:
my $text = do {local $/; <$XML>};
方法二:
sysread $fh, $text, -s $fh;
方法三: 使用Perl6::Slurp模块
use Perl6::Slurp;
sub test {
# Slurp file by name
my $xml_file = 'EJV.xml';
my $text = slurp $xml_file;
print $text, "\n";
# Slurp file by handle
my $xml_file = 'EJV.xml';
open my $XML, '<', $xml_file or die $!;
my $text = slurp $XML;
print $text, "\n";
}
获取引用参数并解引用为数组,shift前面的加号is just a placeholder to let perl know 'shift' is not a regular string.
sub bar {
return @{ shift() };
#return @{ +shift };
}
调用
sub test {
my @a = (1, 2, 3);
my @bar = bar(\@a);
}
==