<?xml version="1.0" encoding="windows-1252"?>
<node id="1014777" title="RFC: A closure guard with overall usage statistics" created="2013-01-22 14:55:20" updated="2013-01-22 14:55:20">
<type id="120">
perlmeditation</type>
<author id="826278">
Dallaylaen</author>
<data>
<field name="doctext">
&lt;p&gt;Not long ago I was hunting a memory leak in a long-running AnyEvent-based application. I thought it would be a great idea to put a special guard object into every closure, so that I could see them created and destroyed.&lt;/p&gt;

&lt;p&gt;The interface was very simple:&lt;/p&gt; 

&lt;pre&gt;
    # in initial section
    use My::Stat::Guard;
    my $stat = My::Stat::Guard-&gt;new;

    # when running
    my $guard = $stat-&gt;guard;
    my $callback = sub { 
        $guard-&gt;finish("taken route 1"); 
        # now do useful stuff
    };
    # ... do whatever I need and call $callback eventually

    # in diagnostic procedures started via external event
    my $data = $stat-&gt;get_stat;
    # now I have some useful numbers in %$data
    # total: 100
    # alive: 19
    # dead: 81
    # etc.
&lt;/pre&gt;

&lt;p&gt;get_stat also shows data for finished callbacks that still float around, and unfinished ones that left scope (typically both mean something went wrong). &lt;/p&gt;

&lt;p&gt;The module also allows (when used with want_time =&gt; 1 parameter to new) to display (very rough) statistical distribution of guard lifetimes (== total time to process request).&lt;/p&gt;

&lt;p&gt;I have searched CPAN for such a module before rolling out my own, but haven't found anything except several &lt;a href="http://search.cpan.org/~mlehmann/Guard-1.022/Guard.pm"&gt;Guard&lt;/a&gt;-like modules.&lt;/p&gt;

&lt;p&gt;So my question is: does such a module exist, and if not, is there a demand for one? &lt;/p&gt;</field>
</data>
</node>
