ディレクトリ ツリー (コマンド ライン引数で指定) の先頭から開始し、各サブディレクトリを再帰的に移動して、各ファイルに対して特定のアクションを実行する Perl スクリプトを作成したいと考えています。
私はfinddepth
これを使用していますが、ベースディレクトリから2レベル以上離れたディレクトリでスクリプトを実行すると機能しないようです。
これが私のコードです:
#!/usr/local/bin/perl -w
use strict;
use File::Copy;
use File::Find;
use File::Basename;
use File::Path;
finddepth(\&file_list, @ARGV);
sub file_list {
my ($file_path, $name, $path, $suffix);
$file_path = $File::Find::name;
($name, $path, $suffix) = fileparse($file_path, /\.*/);
my $fullname = $name . $suffix;
my $file = $fullname;
if ($file =~ /^[^\.].*[^\.pl]$/) {
copy($file, "$file.orig");
open(FILE, "$file");
my @file_data = <FILE>;
close(FILE);
open(FOUT, ">$file") or die " \n File cannot be opened !";
foreach my $line (@file_data) {
if ($line =~ /^\s+Error:/) {
$line =~ s/([^-]\d+)/ \*\*/gc;
print FOUT $line;
}
else {
print FOUT $line;
}
}
close(FOUT);
}
}
次の警告/エラーが一貫してスローされます。
- 閉じたファイルハンドルを読む
- ファイルを開けません!
なぜこれが起こっているのか理解できないようです。質問をできるだけ具体的にしようとしました。さらに情報が必要な場合はお知らせください。ありがとうございました。