.html 파일을 읽는 Perl 프로그램이 있고 프로그램이 .html 파일과 같은 디렉토리에있는 경우에만 작동합니다.
다른 디렉토리에서 시작하여 html의 위치를 매개 변수로 전달할 수 있기를 바랍니다. 프로그램 (아래의 셸 예제)은 서브 디렉토리 "sub" 과 하위 디렉토리를 탐색하여 .html 파일을 찾지만 내 perl 파일이 동일한 서브 디렉토리 "sub"에있을 때만 작동합니다. Perl 파일 을 하위 디렉토리 "sub"의 한 단계 뒤로 홈 디렉토리에 넣으면 작동하지 않습니다.Perl에서 서브 디렉토리를 매개 변수로 전달하려고 시도합니다.
셸에서 "perl project.pl ./sub"를 내 홈 디렉토리에 입력하면 은 열리지 않습니다 ./sub/file1.html. 해당 파일이나 디렉토리가 없습니다. 그러나 파일은 정확한 위치에 존재합니다. file1.html은 읽으려고하는 첫 번째 파일입니다.
쉘의 디렉토리를 해당 서브 디렉토리로 변경하고 .pl 파일을 옮긴 다음 쉘에서 "perl project.pl ./"라고 말하면 모든 것이 정상입니다.
이 파일을 사용하여 내가왔다, 디렉토리를 검색하려면 : 내가 여기 개념을 찾기 : How to traverse all the files in a directory; if it has subdirectories, I want to traverse files in subdirectories too Find::File to search a directory of a list of files$File::Find::name
현재 디렉토리에 상대적으로 더 이상하지 않도록
find
자동으로 디렉토리를 변경
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find;
find(\&directories, $ARGV[0]);
sub directories {
$_ = $File::Find::name;
if(/.*\.html$/){#only read file on local drive if it is an .html
my $file = $_;
open my $info, $file or die "Could not open $file: $!";
while(my $line = <$info>) {
#perform operations on file
}
close $info;
}
return;
}
완벽하게 작동하는 것을 제거해 주셔서 감사합니다. – com