Reputation: 71
So I have some code and I can use it in terminal fine but I can't figure out how to get multiple files for Mojolicious from a directory rather than feeding them 1 by 1. I'm super new to perl and could use excel to make 2,000 lines and pass it in terminal but I'd rather not. Any help is greatly appreciated. Here's the code:
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";
}
Any help is greatly appreciated.
There was a comment below about what to use and I've tried the first method, still don't quite get glob. Here's what I already tried and errored out on:
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";
}
}
I think I need to string but use the same one and mess something up. Thanks again for assisting the newbie.
Upvotes: 1
Views: 880
Reputation: 35198
You failed to include the directory information in your output file:
open my $fh, '>', "${html_files}result.txt" or die $!;
I would recommend revamping your code to use Path::Class
to handle the file and directory operations for you in a cross platform compatible way.
Note, it's not entirely clear what you're trying to do with your code, but this is probably what you're aiming for stylistically:
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 ?
}
}
Upvotes: 1