File Coverage

blib/lib/UNIVERSAL/new.pm
Criterion Covered Total %
statement 26 27 96.3
branch 3 4 75.0
condition 2 6 33.3
subroutine 7 7 100.0
pod n/a
total 38 44 86.3


line stmt bran cond sub pod time code
1 2     2   23948 use 5.008001;
  2         8  
  2         79  
2 2     2   11 use strict;
  2         4  
  2         61  
3 2     2   11 use warnings;
  2         4  
  2         112  
4              
5             package UNIVERSAL::new;
6             # ABSTRACT: Load a module when you call the new method on it
7             our $VERSION = '0.001'; # VERSION
8              
9 2     2   11 use Carp;
  2         3  
  2         215  
10 2     2   1872 use Module::Runtime qw/require_module/;
  2         3725  
  2         16  
11              
12             sub _new {
13 4     4   18543 my ($class) = @_;
14 4   33     28 $class = ref $class || $class;
15 4         8 eval { require_module($class) };
  4         15  
16 4 100       1449 croak($@) if $@;
17 2         4 my $ctor;
18 2 50 33     51 if ( $ctor = $class->can('new') and $ctor != \&UNIVERSAL::new::_new ) {
19 0         0 goto $ctor;
20             }
21             else {
22 2         234 croak(qq{Can't locate object method new via package "$class"});
23             }
24             }
25              
26 2     2   301 no warnings 'once';
  2         5  
  2         133  
27             *UNIVERSAL::new = \&_new;
28              
29             1;
30              
31              
32             # vim: ts=4 sts=4 sw=4 et:
33              
34             __END__