![Perl 코드는 동일한 확장자를 가진 여러 파일(.xml)을 참조합니다.](https://linux55.com/image/6561/Perl%20%EC%BD%94%EB%93%9C%EB%8A%94%20%EB%8F%99%EC%9D%BC%ED%95%9C%20%ED%99%95%EC%9E%A5%EC%9E%90%EB%A5%BC%20%EA%B0%80%EC%A7%84%20%EC%97%AC%EB%9F%AC%20%ED%8C%8C%EC%9D%BC(.xml)%EC%9D%84%20%EC%B0%B8%EC%A1%B0%ED%95%A9%EB%8B%88%EB%8B%A4..png)
여러 XML 파일을 구문 분석하려고 하는데 약간의 문제가 있습니다. 아래 스크립트 섹션은 $twig->parsefile("output_1.xml");
해당 파일을 잘 구문 분석합니다. 그러나 디렉터리의 모든 .xml 파일을 구문 분석하고 싶습니다. 나는 다음과 같은 코드로 각 파일을 작성해 보았습니다.
$twig->parsefile("output_1.xml" "output_2.xml" "output_3.xml");
또한 Bash에서와 같이 와일드카드를 사용해 보았습니다.
$twig->parsefile("*.xml");
그러나 이러한 시도 중 어느 것도 성공하지 못했습니다. 디렉터리의 모든 .xml 파일을 구문 분석하도록 스크립트를 어떻게 변경합니까? 지식이 부족해서 죄송합니다. 저는 이제 막 Perl을 사용하기 시작했습니다.
#!/bin/perl -w
use strict;
use XML::Twig;
my $twig = XML::Twig->new(
twig_handlers => {item => \&acct}
);
$twig->parsefile("output_1.xml");
sub acct {
my ($t, $elt) = @_;
for my $tag (qw(itemId title viewItemURL convertedCurrentPrice)) {
print $elt->field($tag), "\n";
}
print "\n";
print "\n";
}
답변1
다음과 같이 디렉터리 내용을 열거할 수 있습니다.
my @files = <*.xml>; # or: my @files = </your/path/to/*.xml>;
foreach my $file (@files) {
$twig->parsefile($file);
}