【发布时间】:2019-12-08 02:21:47
【问题描述】:
我有一个 PERL 脚本,它应该对文件夹中包含的文本文件执行“某些操作”。从脚本名称我可以猜出应该做什么,但在我看来没有做任何事情(当我从 Windows 上的 git bash shell 执行时,我看不到对文本文件的任何更改)。
我想知道的是,如果在现实中做某事,以及使用另一个 perl 解释器或操作系统的结果是否会有所不同。
#!perl
$|++;
use strict;
use File::Find;
my ($cnt, $uxcnt);
print "Doing something on files on @ARGV ...";
find(\&wanted, @ARGV);
print " done something for ($uxcnt/$cnt) files\n";
sub wanted {
$cnt++;
my $file = $_;
if(-T $file) {
$uxcnt++;
open FILE, $file or warn "Warning: Couldn't open $file: $!\n";
my @buf;
push @buf, $_ while <FILE>;
close FILE;
open FILE, ">$file" or warn "Warning: Couldn't open $file: $!\n";
binmode FILE;
print FILE @buf;
close FILE;
}
}
【问题讨论】:
-
它会做一些奇怪的事情(报告无法打开文件但然后像成功一样继续运行?)以及不推荐使用的旧习语(裸词文件句柄而不是词法,不使用三个参数
open,没有use warnings;,等等...至少它有use strict;) 但它看起来的意图是将Windows 行尾转换为Unix 行尾。