Actually, I think that goto will clean up the stack. My "evidence" for this is:

#! perl -slw use strict; sub a{ my( @args ) = @_; goto MAIN unless @args; a( @args[ 100 .. $#args ] ); return 1; } printf 'Check ram'; <STDIN>; a( 1 .. 10000 ); MAIN: printf 'Check ram'; <STDIN>; my @a = ( 1 .. 50000 ); printf 'Check ram'; <STDIN>;

At the second ram check, there has been substantial growth in the process memory (as expected) through the 100 or so chunks of ram littering the stack.

But allocating a new large array after the goto shows barely any increase. That's leads me to think that the statement in perlsub on goto's "It can be used to go almost anywhere else within the dynamic scope, including out of subroutines," means that the stack is cleaned up.

Whether it's the Right Way for your application is a different matter. The last para of the same section says:

In almost all cases like this, it's usually a far, far better idea to use the structured control flow mechanisms of next, last, or redo instead of resorting to a goto. For certain applications, the catch and throw pair of eval{} and die() for exception processing can also be a prudent approach.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

In reply to Re: How can I return to main loop, not to caller? by BrowserUk
in thread How can I return to main loop, not to caller? by SEI

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.