File Coverage

blib/lib/Sub/SingletonBuilder.pm
Criterion Covered Total %
statement 16 16 100.0
branch 4 4 100.0
condition 4 6 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 30 33 90.9


line stmt bran cond sub pod time code
1             package Sub::SingletonBuilder;
2              
3 1     1   732 use strict;
  1         2  
  1         30  
4 1     1   5 use warnings;
  1         1  
  1         32  
5              
6 1     1   12 use base qw/Exporter/;
  1         2  
  1         200  
7              
8             our @EXPORT = qw/build_singleton/;
9             our $VERSION = '0.02';
10              
11             sub build_singleton {
12 2     2 0 691 my ($ctor, $dtor) = @_;
13 2         4 my $instance = undef;
14             my $getter = sub {
15 5   66 5   1227 $instance ||= $ctor->();
16 2         4 };
17             wantarray && $dtor
18             ? (
19             $getter,
20             sub {
21 2 100   2   781 $dtor->($instance) if $instance;
22 2         7 $instance = undef;
23             },
24             )
25 2 100 66     16 : $getter;
26             }
27              
28             1;
29             __END__