Dear Monks,
The following code aims to extract all url from html files located in the local directory (yes, bad practice... I promise to change that later) After figuring HTML::SimpleLinkExtor might help me, I realized I just didn't know what I was doing. Actually I know: it is called "cargo cult(ing)"... Anyways, I am still labouring over chapter 12 of the Camel book trying to understand how to use OO stuff. I would be grateful if anyone was kind enough to offer a few pointers on what is causing the script to return only the file names, apparently failing to open any of the files.
use strict;
use warnings;
use HTML::SimpleLinkExtor;
my $file;
my $FileOut = "output.txt";
open FileDOut, ">$FileOut" or die "Can't open output file $FileOut: $!
+";
print "Summary written in file $FileOut\n";
opendir(DIR, ".");
my @files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
if ($file =~ /^(.*)htm/i){
print FileDOut "$file\n";
open my ($fh),"<",$file or die "Couldnot do it: $!";
my $extor = HTML::SimpleLinkExtor->new();
$extor->parse_file(<$fh>);
print FileDOut "$_\n";
}
}
The messages I get are:
Unsuccessful open on filename containing newline at C:/Perl/lib/HTML/Parser.pm line 95...
and
Use of uninitialized value $_ in concatenation (.) or string at line 26.Regards & thanks
UPDATE------------
Thanks for the valuable advice, I am progressing but not quite there yet. After modifying the script according the following posts, I now have to figure why the successive
@links appear the right size but are empty. I get as many warnings:
Use of uninitiated value $links[3] in join or string as I have links. But many thanks for clearing up the method invocation and other advice!
UPDATE-2------------
I did not quite make it but I finally resorted to
HTML::LinkExtractor and recipe 20.3 of the Perl Cookbook.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.