File Coverage

blib/lib/Devel/UseAnyFunc.pm
Criterion Covered Total %
statement 41 45 91.1
branch 10 18 55.5
condition 1 2 50.0
subroutine 7 7 100.0
pod n/a
total 59 72 81.9


line stmt bran cond sub pod time code
1             package Devel::UseAnyFunc;
2              
3 1     1   815 use strict;
  1         3  
  1         53  
4 1     1   6 use vars qw($VERSION $DIGANOSTICS);
  1         3  
  1         62  
5 1     1   17 use Carp;
  1         2  
  1         105  
6              
7             BEGIN {
8 1     1   2 $VERSION = 1.00;
9 1   50     85 $DIGANOSTICS ||= 0;
10             }
11              
12             sub import {
13 4     4   549 my ( $self, $name, @sources ) = @_;
14              
15 4 50       13 if ( $name eq '-isasubclass' ) {
16 0         0 my $subclass = ( caller )[0];
17 1     1   15 no strict 'refs';
  1         3  
  1         341  
18 0         0 unshift @{"$subclass\::ISA"}, $self;
  0         0  
19 0         0 return;
20             }
21            
22 4 50       10 $name or croak( "$self called without a function name" );
23 4 50       10 scalar(@sources) or croak("$self $name called without any function sources");
24 4 50       11 scalar(@sources) % 2 and croak("$self name called with odd number of source ".
25             "arguments, should be key-value pairs" );
26            
27             # Find the first class in the caller() stack that's not a subclass of us
28 4         6 my $target;
29 4         4 my $i = 0;
30 4         5 do { $target = ( caller($i ++) )[0] }
  4         53  
31             while UNIVERSAL::isa($target, __PACKAGE__ );
32            
33 4         17 my @candidates = my %candidates = @sources;
34 4         16 while ( my ($class, $function) = splice @candidates, 0, 2 ) {
35 5         16 (my $pm = "$class.pm") =~ s{::}{/}g;
36 5 50       12 warn "Attempting to load $pm...\n" if $DIGANOSTICS;
37 5         7 eval { require $pm };
  5         984  
38 5 50       15 if ( $@ ) { warn "Failed to load $pm: $@" if $DIGANOSTICS; next }
  2 100       7  
  2         9  
39 3 50       8 warn "Installing $class\::$function as $target\::$name\n" if $DIGANOSTICS;
40 1     1   5 no strict 'refs';
  1         2  
  1         163  
41 3         36 return *{"$self\::$name"} = *{"$target\::$name"} =
  3         12  
  3         10  
42 3 50       9 ref($function) ? $function : \&{"$class\::$function"};
43             }
44 1         260 croak( "Can't locate any module to provide $name: " .
45             join(', ', keys %candidates) . ")" )
46             }
47              
48             1;
49              
50             __END__