emimenidecretese:

I'll apologize up front--I'm not answering your question. Instead, I'm going to provide a couple comments on your code.

After applying these two suggestions to your subroutine, I get this:

############################################################ #specifica cosa deve fare la subroutine edit ############################################################ sub edit { my $file = $_; # only operate on html files if ((-e $file) && (! -d $file) && (/.html?/)){ open (FH, "<",$file) || die $!; my $tree = HTML::Tree->new(); $tree->parse_file($file) || die $!; # The main div contains the post of interest my $getmaindiv = $tree->look_down(_tag => "div",id => "post_princ +ipale") || die $!; print $getmaindiv->as_HTML, "\n"; close FH; } }

Most of your subroutine is inside an if statement. In cases like this, I prefer[*] to simply return if the case isn't met, then you save an indentation level, reducing the visual complexity a bit.

sub edit { my $file = $_; # only operate on html files return unless (-e $file) && (! -d $file) && (/.html?/); open (FH, "<",$file) || die $!; my $tree = HTML::Tree->new(); $tree->parse_file($file) || die $!; # The main div contains the post of interest my $getmaindiv = $tree->look_down(_tag => "div",id => "post_princ +ipale") || die $!; print $getmaindiv->as_HTML, "\n"; close FH; }

Now that the code is a little easier to read, I notice that you're not actually using the file handle you open. You're using the HTML parsers ability to accept a filename instead of a file handle. So I'd just remove the file handle code:

sub edit { my $file = $_; # only operate on html files return unless (-e $file) && (! -d $file) && (/.html?/); my $tree = HTML::Tree->new(); $tree->parse_file($file) || die $!; # The main div contains the post of interest my $getmaindiv = $tree->look_down(_tag => "div",id => "post_princ +ipale") || die $!; print $getmaindiv->as_HTML, "\n"; }

[*] Just one of my preferences. Of course all of my suggestions are based on my preferences, but the other ones are pretty-well accepted, while this one is the most discretionary. Since I'm just another programmer among many, take it with a grain of salt.

I hope you find some of this useful...

Update: I specifically said "don't use prototypes", yet I left the prototype in all versions...removed.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Scrape a blog: a statistical approach by roboticus
in thread Scrape a blog: a statistical approach by epimenidecretese

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.