File Coverage

blib/lib/Package/Watchdog/Util.pm
Criterion Covered Total %
statement 75 75 100.0
branch 14 16 87.5
condition 3 3 100.0
subroutine 18 18 100.0
pod 7 8 87.5
total 117 120 97.5


line stmt bran cond sub pod time code
1             package Package::Watchdog::Util;
2 8     8   136730 use strict;
  8         20  
  8         286  
3 8     8   43 use warnings;
  8         16  
  8         251  
4 8     8   43 use base 'Exporter';
  8         23  
  8         1581  
5              
6             #{{{ POD
7              
8             =pod
9              
10             =head1 NAME
11              
12             Package::Watchdog::Util - Utility functions for use within Package::Watchdog objects.
13              
14             =head1 DESCRIPTION
15              
16             Collection of utility functions. All functions in the package are exported.
17              
18             =head1 FUNCTIONS
19              
20             =over 4
21              
22             =cut
23              
24             #}}}
25              
26             # Use the get_all_subs function to list all functions as exportable.
27             our @EXPORT = get_all_subs( __PACKAGE__ );
28              
29             =item build_accessors( @ACCESSOR_LIST )
30              
31             Create an accessor method for each accessor name passed in. Accessors store and
32             retrieve data from $self->{ $accessor }. Determines which package the accessors
33             should be added to via caller().
34              
35             =cut
36              
37             sub build_accessors {
38 19     19 1 706 my ( $package ) = caller();
39 19         410 for my $accessor ( @_ ) {
40 57         702 my $ref = $package . '::' . $accessor;
41             {
42 8     8   44 no strict 'refs';
  8         23  
  8         3111  
  57         68  
43 57         366 *{ $ref } = sub {
44 1104     1104   1418 my $self = shift;
45 1104 100       2170 $self->{ $accessor } = shift( @_ ) if @_;
46 1104         4133 return $self->{ $accessor };
47             }
48 57         207 };
49             }
50             }
51              
52             =item expand_subs( $package, $subs )
53              
54             Takes a package and list of subs, if the list is empty or undefined than all
55             subs in the package will be returned. If the list contains '*' then the return
56             will be all the susb in the list in addition to all the subs in the package.
57              
58             Note: All subs in a package does not include inherited subs.
59              
60             =cut
61              
62             sub expand_subs {
63 49     49 1 77 my ( $package, $subs ) = @_;
64              
65 49 100 100     199 return [ get_all_subs( $package ) ] unless $subs and @$subs;
66 33 100       53 return $subs unless ( grep { $_ eq '*' } @$subs );
  54         224  
67              
68 4         8 my $listed = [ grep { $_ ne '*' } @$subs ];
  5         12  
69 4         11 my $discovered = [ get_all_subs( $package )];
70              
71 4         13 return combine_subs( $listed, $discovered );
72             }
73              
74             =item $subs = combine_subs( $setA, $setB )
75              
76             Combine 2 arrayrefs of sub names into one arrayref containign entirely unique
77             items.
78              
79             =cut
80              
81             sub combine_subs {
82 5     5 1 3454 my ( $setA, $setB ) = @_;
83 5         14 my %combined = map { $_ => 1 } @$setA, @$setB;
  27         74  
84 5         1472 return [ keys %combined ];
85             }
86              
87             =item @list = get_all_subs( $package )
88              
89             Returns all the sub names in the package.
90              
91             =cut
92              
93             sub get_all_subs {
94 31     31 1 71 my ( $package ) = @_;
95 31 50       122 $package = $package . '::' unless $package =~ m/::$/;
96             {
97 8     8   567 no strict 'refs';
  8         16  
  8         1208  
  31         44  
98 31         165 return grep { defined( *{$package . $_}{CODE} )} keys %$package;
  242         230  
  242         881  
99             }
100             }
101              
102             =item %subs = copy_subs( $package, $subs )
103              
104             Get references to all the specified subs in the specified package.
105              
106             $subs must be specified, it will not default to all susb in package.
107              
108             return datastructure:
109              
110             %subs = (
111             name => ref,
112             ...
113             );
114              
115             =cut
116              
117             sub copy_subs {
118 1     1 1 3 my ( $pkg, $subs ) = @_;
119 1         3 return map { $_ => copy_sub($pkg, $_) } @$subs;
  3         8  
120             }
121              
122             =item $ref = copy_sub( $package, $sub )
123              
124             Get the coderef for the specified sub in the specified package. If the sub is
125             inherited, a ref to it will still be returned.
126              
127             =cut
128              
129             sub copy_sub {
130 87     87 1 5275 my ( $pkg, $sub ) = @_;
131 8     8   41 no strict 'refs';
  8         30  
  8         848  
132 87         152 $pkg =~ s/\:\:$//g;
133 87         706 return $pkg->can( $sub );
134             }
135              
136             =item set_sub( $package, $sub, $new )
137              
138             $new should be a coderef.
139              
140             If $new is undef or omitted, the specified sub will be deleted from the
141             package.
142              
143             =cut
144              
145             sub set_sub {
146 116     116 1 5027 my ( $package, $sub, $new ) = @_;
147 116 50       358 $package = $package . '::' unless $package =~ m/::$/;
148 8     8   38 no strict 'refs';
  8         13  
  8         201  
149 8     8   46 no warnings 'redefine';
  8         13  
  8         339  
150 8     8   37 no warnings 'prototype';
  8         12  
  8         1936  
151 116 100       236 if ( $new ) {
152 113         131 *{$package . $sub} = $new;
  113         949  
153             }
154             else {
155 3         6 undef( &{$package . $sub});
  3         48  
156             }
157             }
158              
159             sub proper_return {
160 83     83 0 180 my ( $want, $sub, @params ) = @_;
161              
162 83 100       222 if ( $want ) {
    100          
163 24         124 my @array = $sub->( @params );
164 24         169 return @array;
165             }
166             elsif( defined( $want )) {
167 6         15 my $scalar = $sub->( @params );
168 6         34 return $scalar;
169             }
170              
171 53         148 $sub->( @params );
172 22         72 return;
173             }
174              
175             1;
176              
177             __END__