For example, the following code continually allocates memory blocks at the same two locations. This is not related to my since we're forcing my to continually reallocate SVs by keeping the refcount greater than 1. There is no reuse of allocated memory at all.
perl -le"sub f{ \my $x } print f() for 1..10;"
What refcount is kept greater than 1? After the print, the result of f() is no longer reference to. Hence the ref to $x goes out of scope, and since nothing is refering to $x, that one goes out of scope as well. Here's a slight variation that shows this:
sub DESTROY {say "DESTROYED"} sub f{ bless \my $x } say f() for 1..10; __END__ main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED
OTOH, if you do keep the refcount above 0, no address is reused:
my @a; sub f{ push @a, \my $x; $a[-1] } say f() for 1..10; __END__ SCALAR(0x9c34108) SCALAR(0x9c33c1c) SCALAR(0x9c41f8c) SCALAR(0x9c33f50) SCALAR(0x9c341a8) SCALAR(0x9c34180) SCALAR(0x9c33f00) SCALAR(0x9c50534) SCALAR(0x9c5050c) SCALAR(0x9c504e4)

In reply to Re^5: Reference of constants and literals by JavaFan
in thread Reference of constants and literals by LanX

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.