File Coverage

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


line stmt bran cond sub pod time code
1             package UNIVERSAL::dump;
2              
3             # version info
4             $VERSION= '0.06';
5              
6             # be as strict and verbose as possible
7 1     1   913 use strict;
  1         2  
  1         36  
8 1     1   6 use warnings;
  1         3  
  1         108  
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 9     9   1775 my $class= shift;
34              
35             # make sure default is set
36 9 50       21 unshift( @_,'dump' ) unless @_;
37              
38             # allow for redefining subroutines
39 1     1   20 no warnings 'redefine';
  1         2  
  1         234  
40              
41             # handle all simple specifications
42             METHOD:
43 9         12 foreach my $spec (@_) {
44 12 100       26 if ( !ref($spec) ) {
45 4 50       11 die qq{Don't know how to install method "UNIVERSAL::$spec"\n}
46             unless my $sub= $preset{$spec};
47 4         16 $class->import( { $spec => $sub } );
48 4         17 next METHOD;
49             }
50              
51             # all methods
52 8         10 foreach my $method ( keys %{$spec} ) {
  8         20  
53 8         11 my $sub= $spec->{$method};
54 8 100       17 $sub= $preset{$sub} if $preset{$sub};
55              
56             # already installed
57 8 100       17 if ( my $installed= $installed{$method} ) {
58 2 100       13 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 7         32 ( my $module= $sub ) =~ s#::[^:]+$##;
65 7         12 $module =~ s#::#/#;
66 7         8 $module .= ".pm";
67 7         13 $installed{$method}= $sub;
68              
69             # install the method in the indicated namespace
70 1     1   5 no strict 'refs';
  1         2  
  1         192  
71 7         82 *{"UNIVERSAL::$method"}= sub {
72 3     3   14542 my $self = shift;
73 3         5 eval { require $module };
  3         512  
74 3 100       39 return $sub->( @_ ? @_ : $self ) if defined wantarray;
    50          
75 0 0         print STDERR $sub->( @_ ? @_ : $self );
76             } #UNIVERSAL::$method
77 7         22 }
78             }
79             } #import
80              
81             #---------------------------------------------------------------------------
82              
83             __END__