I recently had a similar issue with hash references and sub-routine recursion and explored two solutions :-

  1. The first solution was to use a single blessed hash which was referenced through each iteration of the subroutine. An example of this solution can be seen at Local::SiteRobot - a simple web crawling module - In particular, have a look at the _crawl subroutine where this technique has been employed.
  2. The second solution involved the shift from a recursion based iteration through elements to stack-based iteration. This technique meant that the recursion process, deemed most evil depending on whom you talk to, could be replaced in its entirity. An example of such code can be seen in the File::Find module shipped with Perl 5.6 and later (which can be compared to the recursion based File::Find module shipped with earlier distributions).

    An example of stack-based code may look like this:

    foreach my $element (@list) { # ... process element, search for sub-elements ... splice ( @list, ($depth_first_processing) ? 0 : @list, 0, $subelement ); }

    A quick note on this code - The variable $depth_first_processing allows new sub-elements can be either added to the end of the list (breadth-first iteration) or as elements to be iterated through within the next loop (depth-first iteration).

    This structure of stack-based iteration has been incorporated into the current version of WWW::SimpleRobot.

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'


In reply to Re: Recursion + hash by rob_au
in thread Recursion + hash by karmas

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.