【问题标题】:how to get files for Mojolicious from a directory如何从目录中获取 Mojolicious 的文件
【发布时间】: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";
    }
}

我想我需要串起来,但使用相同的串并弄乱了一些东西。再次感谢您对新手的帮助。

【问题讨论】:

  • 听起来你只是在问如何读取目录?你没见过readdirglob吗?
  • 是的,我尝试过类似的方法,但它充满了错误。我会发布我尝试过的内容。

标签: perl file input mojolicious


【解决方案1】:

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

    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 ?
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2019-08-14
    • 2012-03-11
    相关资源
    最近更新 更多