Hi,

You can probably help me even if you don't know details of HTML::TreeBuilder or of my application. My question is more general Perl, but framed in my own world. As usual, I can solve this via a very brute force method, but "there has to be a better way".

I'm using HTML::TreeBuilder to grab the filename of an anchor that is an immediate child of a known tag. The structures look like:

... <span class=inst> <a href="file23.html#some_tag">...</a> </span> ...
I want to return a list that contains all the files from key sections like this. For example, the section above should simply "push(@list, "file23.html");"

The snippet below will find the span containing the anchor. I really don't care about that. I just want to push "$1" from the qr// search in the subroutine call into the list.

foreach $inst (@instances) { my @junk = $inst->look_down( 'tag' => 'span', 'class' => 'inst', sub { $_[0]->look_down( '_tag' => 'a', 'href' => qr/(\w+\.html)#\w/)); # ^^^^^^^^^ });
My brute force method is to find all the anchors that fit my pattern and look at their immediate parent to see if they have the "span" tag that interests me, pulling the filename out if it does.

Oh, and the subroutine itself must continue to return a "1" or the look_down() will stop at that point and I won't get all the files.

Thanks for your help!


In reply to 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.