4
如果沒有文件名提供給perl腳本,尋找使用DATA句柄的方法。如何在perl中沒有提供文件名時使用__DATA__句柄
我不是很熟練的perl。
喜歡的東西:
use strict;
use warnings;
use autodie;
use diagnostics;
my $fd;
if($ARGV[0] && -f $ARGV[0]) {
open $fd, "<", $ARGV[0];
} else {
$fd = "DATA"; #what here?
}
process($fd);
close($fd); #closing the file - or the DATA handle too?
sub process {
my $handle = shift;
while(<$handle>) {
chomp;
print $_,"\n";
}
}
__DATA__
default content
這應該是'如果(@ARGV && -f $ ARGV [0]){...'或'如果(定義$ ARGV [0] && -f $ ARGV [0] ){...'(如果有人給你一個名爲'0'的文件會怎麼樣) – 2013-05-07 01:38:00