I don't want to protect $x. I just want to touch $x (store the value into it) when Scope::Guard object gets destroyed, i.e. when all references to it go out of scope.

In simple and obvious example below, the $g gets destroyed and inner sub called immediately after it goes out of scope.

use strict; use Scope::Guard; my $x; { my $g = Scope::Guard->new(sub {$x; warn "destroyed"}); } warn "end";
But I checked, and putting braces around the dcl of $x, doesn't change the output. So I would bet scopeguard has stored a reference to the interior sub so it can be called when "$x" goes out of scope.

Your bet is wrong. The sub is called the guard gets destroyed, not the object it references. Look at the Scope::Guard code. Is very short and self explanatory.

All you did by assigning undef to $code is lose the reference to the outside sub.. SGnew still has a reference to the inner sub and $x...

Yes, but when I lose reference to the outside sub, I should also lose the last reference to $g, because the sub is the only object which sees $g. But it doesnot happen.

Does that make more sense?

I need to read comments above, but it still doesnot make more sense to me. However constructed is the inner sub (stored in $g) it is just another object referenced by $g, not referencing it. So I don't understand how it (the inner sub) can affect the reference count to $g.

The example I gave doesnot make sense itself, it is just the simplest demonstration of the behaviour I was able to make up. My original was more like the test below: ($x is \@res, I just try to log into res, when the object gets destroyed).

use strict; use Test::More tests=>2; use Scope::Guard; sub try { my @res; my @subs = do { my $g; ( sub { push @res, 'a'; $g = Scope::Guard->new( sub { warn "destroyed"; push @res, 'destroyed' } ); }, sub { push @res, 'b'; undef $g; } ); }; ( shift @subs )->(); is_deeply( \@res, ['a'] ); undef @subs; is_deeply( \@res, [ 'a', 'destroyed' ] ); } try();

In reply to Re^4: Reference to guard not released by roman
in thread Reference to guard not released by roman

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.