File Coverage

blib/lib/UNIVERSAL/dump.pm
Criterion Covered Total %
statement 36 36 100.0
branch 18 18 100.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 60 60 100.0


line stmt bran cond sub pod time code
1             package UNIVERSAL::dump;
2              
3             # version info
4             $VERSION= '0.10';
5              
6             # be as strict and verbose as possible
7 4     4   1341680 use strict;
  4         10  
  4         165  
8 4     4   20 use warnings;
  4         7  
  4         566  
9              
10             # presets
11             my %preset= (
12             blessed => 'Scalar::Util::blessed',
13             dump => 'Data::Dumper::Dumper',
14             peek => 'Devel::Peek::Dump',
15             refaddr => 'Scalar::Util::refaddr',
16             );
17              
18             # installed handlers
19             my %installed;
20              
21             # satisfy require
22             1;
23              
24             #-------------------------------------------------------------------------------
25             #
26             # Perl specific subroutines
27             #
28             #-------------------------------------------------------------------------------
29             # IN: 1 class (ignored)
30             # 2..N method => subroutine pairs
31              
32             sub import {
33 21     21   7095 my $class= shift;
34              
35             # make sure default is set
36 21 100       89 unshift( @_,'dump' ) unless @_;
37              
38             # allow for redefining subroutines
39 4     4   29 no warnings 'redefine';
  4         11  
  4         1355  
40              
41             # handle all simple specifications
42             METHOD:
43 21         46 foreach my $spec (@_) {
44 27 100       63 if ( !ref($spec) ) {
45             die qq{Don't know how to install method "UNIVERSAL::$spec"\n}
46 10 100       56 unless my $sub= $preset{$spec};
47 9         41 $class->import( { $spec => $sub } );
48 9         78 next METHOD;
49             }
50              
51             # all methods
52 17         29 foreach my $method ( keys %{$spec} ) {
  17         54  
53 17         37 my $sub= $spec->{$method};
54 17 100       71 $sub= $preset{$sub} if $preset{$sub};
55              
56             # already installed
57 17 100       47 if ( my $installed= $installed{$method} ) {
58 4 100       56 die qq{Cannot install "UNIVERSAL::$method" with "$sub":}
59             . qq{ already installed with "$installed"\n}
60             if $sub ne $installed;
61             }
62              
63             # mark method as installed
64 15         122 ( my $module= $sub ) =~ s#::[^:]+$##;
65 15         45 $module =~ s#::#/#;
66 15         28 $module .= ".pm";
67 15         36 $installed{$method}= $sub;
68              
69             # install the method in the indicated namespace
70 4     4   30 no strict 'refs';
  4         8  
  4         979  
71 15         135 *{"UNIVERSAL::$method"}= sub {
72 8     8   32009 my $self = shift;
73 8         19 eval { require $module };
  8         920  
74 8 100       61 return $sub->( @_ ? @_ : $self ) if defined wantarray;
    100          
75 2 100       16 print STDERR $sub->( @_ ? @_ : $self );
76             } #UNIVERSAL::$method
77 15         83 }
78             }
79             } #import
80              
81             #---------------------------------------------------------------------------
82              
83             __END__