One way of addressing the array indices problem is to declare constants for your array elements.

use constant { SELF => 0, CODE_TO_RUN => 1, FIRST_DAUGHTER => 0, LAST_DAUGHTER => 1, NEXT_SISTER => 2, PREVIOUS_SISTER => 3, PARENT => 4, }; ## then in your methods sub traverse { if( $_[SELF][FIRST_DAUGHTER] ) { traverse( $_[SELF][FIRST_DAUGHTER], $_[CODE_TO_RUN] ); } elsif( $_[SELF][NEXT_SISTER] ) { traverse( $_[SELF][NEXT_SISTER], $_[CODE_TO_RUN] ); elsif( ... ) ...; } return; }

Constants created this way get turned into constant subroutines which then get optimised away leaving just the value behind which makes for an efficient way of indexing arrays symbolically.

And by using the aliasing of @_ directly, rather than shifting or copying values into lexicals, you save a little more time and space.

With well chosen constant names, it is also eminently readable, even if the uppercase tends to look a little strange at first.

For those methods (like traverse()) that lend themselves to tail recursion, you can take this further:

sub traverse { return unless defined $_[SELF]; $_[CODE_TO_RUN]->( ... ); $_[SELF] = $_[SELF][FIRST_DAUGHTER] || $_[SELF][NEXT_SISTER] || undef; goto &traverse; }

But some might consider that a step too far.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re^4: Which, if any, is faster? by BrowserUk
in thread Which, if any, is faster? by rvosa

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.