> Or am I complete wrong about scope in the first place,

Indeed!

By returning a reference of a scoped variable you're incrementing it's "reference-counter", such that the variable is not destroyed when leaving the scope.

The variable will be destroyed as soon as you also delete any variable holding that reference, cause the reference counter goes to 0.

Can't give much guidance about how C or C++ behave.

Cheers Rolf

( addicted to the Perl Programming Language)

update

from perlref

       Hard references are smart--they keep track of reference counts for you,
       automatically freeing the thing referred to when its reference count
       goes to zero.  (Reference counts for values in self-referential or
       cyclic data structures may not go to zero without a little help; see
       "Two-Phased Garbage Collection" in perlobj for a detailed explanation.)
       If that thing happens to be an object, the object is destructed.  See
       perlobj for more about objects.  (In a sense, everything in Perl is an
       object, but we usually reserve the word for references to objects that
       have been officially "blessed" into a class package.)

In reply to Re: my and scope by LanX
in thread my, scope, and references by chayashida

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.