This kind of recursion can be rewritten as iteration. Here's some untested code:

my @subcategories = ("Physics"); my %unique_pages; while (@subcategories) { my $next_subcategory = shift @subcategories; my @nodes = fetch_subcategory($next_subcategory); NODE: foreach my $node (@nodes) { if ($node->type() eq "subcategory") { push @subcategories, $node; next NODE; } $unique_pages{$node->unique_id()} = $node; } } foreach my $page (values %unique_pages) { my $page_content = fetch_page($page); # ... stuff here ... }

In POE, you might keep the subcategories list and the unique pages hash in a session's heap. Class-scoped lexicals are also good here, especially if you plan for many sessions to share the burden of crawling. Here's one way to do it:

sub fetch_next_subcategory { my ($kernel, $heap) = @_[KERNEL, HEAP]; my $next_subcategory = shift @{$heap->{subcategories}}; if (defined $next_subcategory) { # ... start a subcategory fetch ... } else { # switch to fetching articles $kernel->yield("fetch_next_article"); } } sub got_a_subcategory_page { my ($kernel, $heap, $page) = @_[KERNEL, HEAP, ARG0]; my @nodes = parse_subcategory($page); NODE: foreach my $node (@nodes) { if ($node->type() eq "subcategory") { push @{$heap->{subcategories}}, $node; } else { push @{$heap->{pages}}, $node; } } $kernel->yield("fetch_next_subcategory"); } sub fetch_next_article { my ($kernel, $heap) = @_[KERNEL, HEAP]; my $next_page; while (defined($next_page = shift @{$heap->{pages}})) { last unless exists $heap->{unique_pages}{$next_page->unique_id()}; } unless (defined $next_page) { # ... done. return; } $heap->{unique_pages}{$next_page->unique_id()} = 1; # ... start fetching next page } sub got_an_article_page { my ($kernel, $heap) = @_[KERNEL, HEAP]; # ... process page. $kernel->yield("fetch_next_article"); }

In reply to Re^3: POE and recursion by rcaputo
in thread POE and recursion by OverlordQ

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.