Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The other day I was writing a recursive subroutine in which I wanted the sub to know the current depth of recursion.

In C, I would have used a local static - asking here in the CB, a kind fellow monk (sorry, I forgot which monk!) gave me the hint to use a 'my $variable' outside of the sub, but inside an enclosing anonymous block.

{ #(1) my $depth = -1; sub recurse { #(2) ++$depth; print +(" .") x $depth . "current depth:$depth\n" ; recurse() if $depth < 3; print +(" .") x $depth . "current depth:$depth\n" ; --$depth; } #(3) } #(4) recurse(); # (5)
The above (trivial) example produces:
current depth:0 .current depth:1 . .current depth:2 . . .current depth:3 . . .current depth:3 . .current depth:2 .current depth:1 current depth:0

Which is great. Exactly what I wanted. The only problem now is - I don't understand HOW it works.?

My (possibly flawed) conceptual understanding is that the sub keyword creates a named (or unnamed in the case of anonymous subs) block of code. That is to say, an entry in a symbol table somewhere with a "key" of "recurse" (in this case) and a value of the address where the compiler stored the code between the opening "{" (2) and its matching "}" (3). And when the label "recurse" is encountered (in the proper context), the label is looked up in the symbol table, the address of the code obtained and invoked.

To my (still very green) concept of these things, when the program is interpreted/complied/run, the my $depth var should come in to being at the top of the enclosing block (1) and disappear at the bottom (4). Meaning it wouldn't exist at the by the time the sub is invoked at (5)?

Anyone care to attempt to explain how this works to me<super>*</super> without reference to perlguts?

<super>*</super>Someone who has read the docs on my and local several times including the space-time explanation and is still somewhat confused.

In reply to sub and anonymous sub by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (7)
    As of 2018-01-24 12:16 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (260 votes). Check out past polls.