如何从目录中获取Mojolicious的文件

时间:2014-10-20 21:16:27

标签: perl file input mojolicious

所以我有一些代码,我可以在终端中使用它,但我无法弄清楚如何从目录中获取Mojolicious的多个文件,而不是将它们1提供给它。我是超级新的to 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";
}

非常感谢任何帮助。

下面有一条关于使用什么的评论,我已经尝试了第一种方法,但仍然没有得到全球化。这是我已经尝试过的错误:

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";
    }
}

我认为我需要使用相同的字符串并将其搞砸。再次感谢您协助新手。

1 个答案:

答案 0 :(得分:0)

您未能在输出文件中包含目录信息:

    open my $fh, '>', "${html_files}result.txt" or die $!;

我建议您修改代码以使用Path::Class以跨平台兼容的方式为您处理文件和目录操作。

请注意,您尝试对代码执行的操作并不完全清楚,但这可能是您在风格上的目标:

use lib '/Users/lialin/perl5/lib/perl5';
use strict;
use warnings;
use feature 'say';

use Mojo::DOM;
use Path::class;
use URI;

my $dir = dir("Ask/");

for my $file ( $dir->children ) {
    next if $file->is_dir || $file !~ /\.html$/i;

    my $data = $html_file->slurp;
    my $dom  = Mojo::DOM->new($data);

    my $fh = file( $file . 'result.txt' )->openw;

    for my $csshref ( $dom->find('a[href]')->attr('href')->each ) {
        my $cssurl = URI->new($csshref)->abs( $file->basename );   # What are you doing with abs ?

        $fh->print("$file\t$_\n");  # <-- What is $_ supposed to be ?
    }
}