File Coverage

blib/lib/Exodist/Util/Package.pm
Criterion Covered Total %
statement 44 47 93.6
branch 6 12 50.0
condition 5 15 33.3
subroutine 11 11 100.0
pod 3 3 100.0
total 69 88 78.4


line stmt bran cond sub pod time code
1             package Exodist::Util::Package;
2 5     5   106039 use strict;
  5         10  
  5         160  
3 5     5   26 use warnings;
  5         9  
  5         120  
4              
5 5     5   24 use Exporter::Declare;
  5         7  
  5         32  
6 5     5   7223 use Carp qw/croak/;
  5         17  
  5         1051  
7              
8             default_exports qw/
9             inject_sub
10             /;
11             exports qw/
12             package_subs
13             package_sub_map
14             /;
15              
16             sub inject_sub {
17 45     45 1 916 my ( $package, $name, $code, $redefine ) = @_;
18              
19 45 50 33     352 croak "You must provide a package name, a sub name, and a coderef"
      33        
20             unless $package && $name && $code;
21              
22 45 50       119 croak "Package must not be a reference (got: $package)"
23             if ref( $package );
24              
25 45 50       93 croak "Sub name must not be a reference (got: $name)"
26             if ref( $name );
27              
28 45 50 33     223 croak "Third argument must be a coderef (got: $code)"
29             unless ref( $code ) && ref( $code ) eq 'CODE';
30              
31 45         117 my $fullname = join( '::', $package, $name );
32              
33 45 50       90 if ( $redefine ) {
34 5     5   27 no strict 'refs';
  5         16  
  5         184  
35 5     5   27 no warnings 'redefine';
  5         8  
  5         278  
36 0         0 *$fullname = $code;
37             }
38             else {
39 5     5   26 no strict 'refs';
  5         8  
  5         530  
40 45         5036 *$fullname = $code;
41             }
42             }
43              
44             sub package_subs {
45 2     2 1 388 my ( $package, $match ) = @_;
46 2   33     8 $package ||= caller;
47 2         3 $package = $package . '::';
48 5     5   26 no strict 'refs';
  5         7  
  5         1238  
49 2         9 my @list = grep { defined( *{$package . $_}{CODE} )} keys %$package;
  6         11  
  6         25  
50 2 50       28 return @list unless $match;
51 0         0 return grep { $_ =~ $match } @list;
  0         0  
52             }
53              
54             sub package_sub_map {
55 1     1 1 3 my ( $package, $match ) = @_;
56 1   33     4 $package ||= caller;
57 1         4 my @list = package_subs( $package, $match );
58 1         4 return map {( $_ => $package->can( $_ ))} @list;
  3         25  
59             }
60              
61             1;
62              
63             =head1 NAME
64              
65             Exodist::Util::Package - Tools for injecting, finding, or mapping subroutines
66             in a given package.
67              
68             =head1 DEFAULT EXPORTS
69              
70             =over 4
71              
72             =item inject_sub( $package, $name, $code, $redefine )
73              
74             Inject $code as the function/method named $name in package $package. $redefine
75             should be set to true if you are intentionally redefining an existing sub.
76              
77             =back
78              
79             =head1 OPTIONAL EXPORTS
80              
81             =over 4
82              
83             =item @list = package_subs( $package )
84              
85             =item @list = package_subs( $package, qr/match/ )
86              
87             Get a list of all subs in a package. The second argument is an optional regex
88             that will be used to filter the list.
89              
90             =item %name_to_sub_map = package_sub_map( $package )
91              
92             =item %name_to_sub_map = package_sub_map( $package, qr/match/ )
93              
94             Get a map of name => coderef for all subs in a package. Second orgumunt is an
95             optional regexp filter.
96              
97             =back
98              
99             =head1 AUTHORS
100              
101             Chad Granum L
102              
103             =head1 COPYRIGHT
104              
105             Copyright (C) 2010 Chad Granum
106              
107             Exodist-Util is free software; Standard perl licence.
108              
109             Exodist-Util is distributed in the hope that it will be useful, but WITHOUT
110             ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
111             FOR A PARTICULAR PURPOSE. See the license for more details.