【发布时间】:2014-10-20 21:16:26
【问题描述】:
所以我有一些代码,我可以在终端很好地使用它,但我不知道如何从一个目录中获取 Mojolicious 的多个文件,而不是一个一个地提供它们。我对 perl 非常陌生,可以使用 excel 制作 2,000 行并将其传递到终端,但我宁愿不这样做。 任何帮助是极大的赞赏。 代码如下:
use lib '/Users/lialin/perl5/lib/perl5';
use strict;
use warnings;
use feature 'say';
use File::Slurp 'slurp'; # makes it easy to read files.
use Mojo;
use Mojo::UserAgent;
use URI;
#my $html_file = "Ask/Agilent_Technologies_ask.html"; # take file from directory
my $html_file = shift @ARGV; # take file from command lin
my $dom = Mojo::DOM->new( scalar slurp $html_file);
print $html_file ;
#for my $csshref ($dom->find('a[href]')->attr('href')->each) {
#for my $link ($dom->find('a[href]')->attr('href')->each) {
# print $1;
#say $1 #if $link->attr('href') =~ m{^https?://(.+?)/index\.php}s;
for my $csshref ( $dom->find('a[href]')->attr('href')->each ) {
my $cssurl = URI->new($csshref)->abs($html_file);
print "$cssurl\n";
}
非常感谢任何帮助。
下面有一条关于使用什么的评论,我已经尝试了第一种方法,但仍然不太了解 glob。以下是我已经尝试过的错误:
use lib '/Users/lialin/perl5/lib/perl5';
use strict;
use warnings;
use feature 'say';
use File::Slurp 'slurp'; # makes it easy to read files.
use Mojo;
use Mojo::UserAgent;
use URI;
#my $html_file = "Ask/Agilent_Technologies_ask.html"; # take file from directory
#my $html_file = shift @ARGV; # take file from command lin
my $calls_dir = "Ask/";
opendir( my $search_dir, $calls_dir ) or die "$!\n";
my @html_files = grep /\.html$/i, readdir $search_dir;
closedir $search_dir;
#print "Got ", scalar @files, " files\n";
#my %seen = ();
foreach my $html_files (@html_files) {
my %seen = ();
my $current_file = $calls_dir . $html_files;
open my $FILE, '<', $current_file or die "$html_files: $!\n";
my $dom = Mojo::DOM->new( scalar slurp $html_files);
print $html_files ;
#for my $csshref ($dom->find('a[href]')->attr('href')->each) {
#for my $link ($dom->find('a[href]')->attr('href')->each) {
# print $1;
#say $1 #if $link->attr('href') =~ m{^https?://(.+?)/index\.php}s;
for my $csshref ( $dom->find('a[href]')->attr('href')->each ) {
my $cssurl = URI->new($csshref)->abs($html_files);
open my $fh, '>', "${html_files}result.txt" or die $!;
$fh->print("$html_files\t$_\n");
#print "$cssurl\n";
}
}
我想我需要串起来,但使用相同的串并弄乱了一些东西。再次感谢您对新手的帮助。
【问题讨论】:
标签: perl file input mojolicious