I really like using Web::Scraper for that, or rather, its method of using HTML::TreeBuilder::XPath and HTML::Selector::XPath to specify and extract tags:

use strict; use Web::Scraper; use Data::Dumper; my $data = do { local $/; <DATA> }; # Weirdo syntax of Web::Scraper my $link = scraper { process 'a', href => '@href', text => 'TEXT'; result 'href', 'text'; }; my $scraper = scraper { process 'span.inst a', 'links[]' => $link; result 'links[]'; }; print Dumper $scraper->scrape($data); __DATA__ <html> <body> ... <span class=inst> <a href="file23.html#some_tag">aaa</a> </span> <span class=inst> <a href="file24.html#some_tag">bbb</a> </span> <span class=no_inst> <a href="file24.html#some_tag">(should not match either due to wron +g span class)</a> </span> <a href="file23.html#some_tag">a bare link (should not match)</a> </body> </html>

Getting the syntax of Web::Scraper right isn't always straightforward (to me at least), but I hope that some better, non-code based, configurability will come soon.


In reply to Re: Saving a Pattern Match from Subroutine by Corion
in thread Saving a Pattern Match from Subroutine by shoness

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.