| 1 | package UNIVERSAL::dump; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # version info | ||||||||||||||
| 4 | 4 | $VERSION= '0.10'; | |||||||||||||
| 5 | |||||||||||||||
| 6 | # be as strict and verbose as possible | ||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | # presets | ||||||||||||||
| 11 | 4 | 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 | 4 | my %installed; | |||||||||||||
| 20 | |||||||||||||||
| 21 | # satisfy require | ||||||||||||||
| 22 | 4 | 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 | ▶ | my $class= shift; | ||||||||||||
|
Subroutine
import: called CC=9 SCAR=22.0
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | # make sure default is set | ||||||||||||||
| 36 | 21 | ▶ | unshift( @_,'dump' ) unless @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | # allow for redefining subroutines | ||||||||||||||
| 39 | 4 | ▶ | no warnings 'redefine'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | # handle all simple specifications | ||||||||||||||
| 42 | METHOD: | ||||||||||||||
| 43 | 21 | foreach my $spec (@_) { | |||||||||||||
| 44 | 27 | ▶ | if ( !ref($spec) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | die qq{Don't know how to install method "UNIVERSAL::$spec"\n} | ||||||||||||||
| 46 | 10 | ▶ | unless my $sub= $preset{$spec}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 9 | $class->import( { $spec => $sub } ); | |||||||||||||
| 48 | 9 | next METHOD; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | # all methods | ||||||||||||||
| 52 | 17 | foreach my $method ( keys %{$spec} ) { | |||||||||||||
| 53 | 17 | my $sub= $spec->{$method}; | |||||||||||||
| 54 | 17 | ▶ | $sub= $preset{$sub} if $preset{$sub}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | # already installed | ||||||||||||||
| 57 | 17 | ▶ | if ( my $installed= $installed{$method} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 4 | ▶ | die qq{Cannot install "UNIVERSAL::$method" with "$sub":} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | . qq{ already installed with "$installed"\n} | ||||||||||||||
| 60 | if $sub ne $installed; | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | # mark method as installed | ||||||||||||||
| 64 | 15 | ( my $module= $sub ) =~ s#::[^:]+$##; | |||||||||||||
| 65 | 15 | $module =~ s#::#/#; | |||||||||||||
| 66 | 15 | $module .= ".pm"; | |||||||||||||
| 67 | 15 | $installed{$method}= $sub; | |||||||||||||
| 68 | |||||||||||||||
| 69 | # install the method in the indicated namespace | ||||||||||||||
| 70 | 4 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 15 | *{"UNIVERSAL::$method"}= sub { | |||||||||||||
| 72 | 8 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||
| 73 | 8 | eval { require $module }; | |||||||||||||
| 74 | 8 | ▶ | return $sub->( @_ ? @_ : $self ) if defined wantarray; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 75 | 2 | ▶ | print STDERR $sub->( @_ ? @_ : $self ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | } #UNIVERSAL::$method | ||||||||||||||
| 77 | 15 | } | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | } #import | ||||||||||||||
| 80 | |||||||||||||||
| 81 | #--------------------------------------------------------------------------- | ||||||||||||||
| 82 | |||||||||||||||
| 83 | __END__ | ||||||||||||||