2014-10-20 17 views
0

因此我有一些代碼,我可以在終端上使用它,但我無法弄清楚如何從目錄中獲取Mojolicious的多個文件,而不是一個接一個地提供它們。我是perl的超級新手,可以使用excel製作2000行並在終端中傳遞,但我寧願不行。 任何幫助,不勝感激。 下面的代碼:如何從目錄中獲取Mojolicious的文件

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

我想我需要字符串,但使用相同的一個,並搞砸了。再次感謝您協助新手。

+0

它幾乎聽起來像你剛纔問如何讀一個目錄?你有沒有看過['readdir'](http://perldoc.perl.org/functions/readdir.html)或['glob'](http://perldoc.perl.org/functions/glob.html)? – Miller 2014-10-20 21:56:44

+0

是的,我嘗試過類似的東西,但它充滿了錯誤。我會發布我的嘗試。 – tlialin 2014-10-20 22:08:17

回答

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 ? 
    } 
} 
相關問題