One detail. Lexical variables do not hand their memory back. Instead perl assumes that it will need to repopulate them again and keeps the memory in use to make it faster to allocate on the next pass through. Unfortunately this can be seen in current versions of Perl in an obscure bug when you declare a my variable inside a one-line if or unless. In particular this sort of thing is seriously unwise:
sub my_func { # Stuff my $foo = $bar unless $cond; # More stuff }
Perl's behaviour in this case is IMO seriously broken, intentionally undocumented, and as Larry Wall put it, "Use of this feature would be erroneous." It is also far more complex than a simple test would indicate.

I would expect it to get fixed, probably for 5.6.2. In the meantime make sure that my statements are guaranteed to be executed.

EDIT
If anyone wants to know more about this, here is an explanation of what exactly is going on, and the original bug report. (*ahem* Yup, that is my bug.)


In reply to RE: RE: Memory usage in Perl. by tilly
in thread Memory usage in Perl. by Speedfreak

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.