Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2014-11-28 00:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (191 votes), past polls