Thank you for posting working code. It is a clear example. I tweaked a little. Not comprehensive but indentation, as shown in previous examples from other monks, in particular is good for recursion examples, I think.

sub foo { # 4 spaces or tab is standard. Why be different here? # Even this spotty arg check is better than none. my $nesting_level = shift || die "Nesting level needed!"; # Indentation is a great way to visualize recursion. my $indent = " " x ( $nesting_level - 1 ); print $indent, "Hello from level $nesting_level!\n"; # It's Perl, why not postfix? foo($nesting_level+1) if $nesting_level < 3; print $indent, "Goodbye from level $nesting_level!\n"; }
Hello from level 1! Hello from level 2! Hello from level 3! Goodbye from level 3! Goodbye from level 2! Goodbye from level 1!

In reply to Re^2: recursion basics by Your Mother
in thread recursion basics by wrinkles

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.