| 1 | package Package::Butcher; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Package::Butcher::Inflator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use constant VALID_PACKAGE_RE => qr/^\w+(?:::\w+)*$/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use constant VALID_SUBROUTINE_RE => qr/^[_[:alpha:]][[:word:]]*$/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 1 | my ( $class, $arg_for ) = @_; | |||||||||||||||||||||||||||
| 16 | 1 | my $self = bless {} => $class; | |||||||||||||||||||||||||||
| 17 | 1 | return $self->_initialize($arg_for); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub _initialize { | ||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 21 | 1 | my ( $self, $arg_for ) = @_; | |||||||||||||||||||||||||||
| 22 | my %default_for = ( | ||||||||||||||||||||||||||||
| 23 | package => delete $arg_for->{package}, | ||||||||||||||||||||||||||||
| 24 | import_on_use => delete $arg_for->{import_on_use}, | ||||||||||||||||||||||||||||
| 25 | 1 | is_package_loaded => 0, | |||||||||||||||||||||||||||
| 26 | subs_installed => {}, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | 1 | foreach my $method ( keys %default_for ) { | |||||||||||||||||||||||||||
| 29 | 4 | $self->{$method} = $default_for{$method}; | |||||||||||||||||||||||||||
| 30 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | *$method = sub { $_[0]->{$method} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 1 | $self->_do_not_load( delete $arg_for->{do_not_load} ); | |||||||||||||||||||||||||||
| 34 | # _sub() must be called before _predeclare | ||||||||||||||||||||||||||||
| 35 | 1 | $self->_subs( delete $arg_for->{subs} ); | |||||||||||||||||||||||||||
| 36 | 1 | $self->_predeclare( delete $arg_for->{predeclare} ); | |||||||||||||||||||||||||||
| 37 | 1 | $self->_method_chains( delete $arg_for->{method_chains} ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | return $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub _is_package_loaded { $_[0]->{is_package_loaded} = $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
_is_package_loaded: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub _assert_looks_like_package { | ||||||||||||||||||||||||||
|
Subroutine
_assert_looks_like_package: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 45 | 4 | my ( $proto, $package ) = @_; | |||||||||||||||||||||||||||
| 46 | 4 | ▶ | unless ( $package =~ VALID_PACKAGE_RE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | Carp::confess( | |||||||||||||||||||||||||||
| 48 | "'$package' does not look like a valid package name to me"); | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 8 | ▶ | sub _assert_looks_like_subroutine { | ||||||||||||||||||||||||||
|
Subroutine
_assert_looks_like_subroutine: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 53 | 8 | my ( $proto, $subroutine ) = @_; | |||||||||||||||||||||||||||
| 54 | 8 | ▶ | unless ( $subroutine =~ VALID_SUBROUTINE_RE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | Carp::confess("'$subroutine' does not look like a valid subroutine name to me"); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub _do_not_load { | ||||||||||||||||||||||||||
|
Subroutine
_do_not_load: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 60 | 1 | my ( $self, $packages ) = @_; | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | return unless $packages; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | $packages = [$packages] unless 'ARRAY' eq ref $packages; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | foreach my $package (@$packages) { | |||||||||||||||||||||||||||
| 64 | 3 | $self->_assert_looks_like_package($package); | |||||||||||||||||||||||||||
| 65 | 3 | my $file = "$package.pm"; | |||||||||||||||||||||||||||
| 66 | 3 | $file =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 67 | 3 | my $butcher = ref $self; | |||||||||||||||||||||||||||
| 68 | 3 | my $message = "loaded via '$butcher'"; | |||||||||||||||||||||||||||
| 69 | 3 | ▶ | if ( $INC{$file} && $INC{$file} ne $message ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$INC{$file} and $INC{$file} ne $message
MC/DC0%0/2
$INC{$file} and $INC{$file} ne $message
$INC{$file}
$INC{$file} ne $message
Truth table33%1/3
A and B
A= $INC{$file}B= $INC{$file} ne $message
| |||||||||||||||||||||||||||||
| 70 | 0 | Carp::cluck("'$package' already loaded via '$INC{$file}'"); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 3 | $INC{$file} = $message; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # This ensures that "use Foo 'bar'" won't generate "package Foo | ||||||||||||||||||||||||||||
| 76 | # doesn't export bar" errors | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 3 | ▶ | *{"${package}::import"} = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | ▶ | sub _subs { | ||||||||||||||||||||||||||
|
Subroutine
_subs: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||
| 84 | 1 | my ( $self, $subs ) = @_; | |||||||||||||||||||||||||||
| 85 | 1 | ▶ | return unless $subs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | my $subs_installed = $self->subs_installed; | |||||||||||||||||||||||||||
| 87 | 1 | foreach my $sub (keys %$subs) { | |||||||||||||||||||||||||||
| 88 | 3 | $self->_assert_looks_like_subroutine($sub); | |||||||||||||||||||||||||||
| 89 | 3 | ▶ | if (exists $subs_installed->{$sub} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | Carp::confess("Cannot install a subroutine already installed: '$sub'"); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 3 | my $code = $subs->{$sub}; | |||||||||||||||||||||||||||
| 93 | 3 | ▶ | unless ( 'CODE' eq ref $code ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | Carp::confess("The value for '$sub' must be a subroutine reference"); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 3 | $subs_installed->{$sub} = $code; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | sub _method_chains { | ||||||||||||||||||||||||||
|
Subroutine
_method_chains: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 101 | 1 | my ( $self, $chains ) = @_; | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | return unless $chains; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 1 | foreach my $chain (@$chains) { | |||||||||||||||||||||||||||
| 104 | 1 | my ( $class_to_override, @methods ) = @$chain; | |||||||||||||||||||||||||||
| 105 | 1 | my $code = pop @methods; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | unless (@methods) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | Carp::confess("Must have at least one method name to call on $class_to_override"); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 1 | ▶ | unless ( 'CODE' eq ref $code ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | Carp::confess("Final argument to install_method_chain must be a coderef"); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | $self->_assert_looks_like_package($class_to_override); | |||||||||||||||||||||||||||
| 115 | 1 | $self->_assert_looks_like_subroutine($_) foreach @methods; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | my $first = shift @methods; | |||||||||||||||||||||||||||
| 118 | 1 | my $inflate = $code; | |||||||||||||||||||||||||||
| 119 | 1 | ▶ | while ( my $method = pop @methods ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 4 | $inflate = { $method => $inflate }; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | *{"${class_to_override}::$first"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 1 | Package::Butcher::Inflator->new($inflate); | |||||||||||||||||||||||||||
| 126 | 1 | }; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 1 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | ▶ | sub _uniq { | ||||||||||||||||||||||||||
|
Subroutine
_uniq: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | 1 | my %seen = (); | |||||||||||||||||||||||||||
| 134 | 1 | grep { not $seen{$_}++ } @_; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | sub _predeclare { | ||||||||||||||||||||||||||
|
Subroutine
_predeclare: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 138 | 1 | my ( $self, $subs ) = @_; | |||||||||||||||||||||||||||
| 139 | 1 | ▶ | return unless $subs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | $subs = [$subs] unless 'ARRAY' eq ref $subs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 1 | my $installed = $self->subs_installed; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # we have to predeclare subs we're installing lest we hit subtle parsing | ||||||||||||||||||||||||||||
| 144 | # issues where Perl thinks they're indirect method calls. See 'perldoc | ||||||||||||||||||||||||||||
| 145 | # perlobj' for more information. Thanks for Flavio for spotting this | ||||||||||||||||||||||||||||
| 146 | # issue. | ||||||||||||||||||||||||||||
| 147 | 1 | @$subs = _uniq(@$subs, keys %$installed); | |||||||||||||||||||||||||||
| 148 | 1 | my $package = $self->package; | |||||||||||||||||||||||||||
| 149 | 1 | my $forward_declarations = join '' => map { "sub $_;" } @$subs; | |||||||||||||||||||||||||||
| 150 | 1 | eval "package $package; $forward_declarations"; | |||||||||||||||||||||||||||
| 151 | 1 | ▶ | Carp::confess($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | sub use { | ||||||||||||||||||||||||||
|
Subroutine
use: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 1 | my ( $self, @import ) = @_; | |||||||||||||||||||||||||||
| 156 | 1 | $self->_load( 'use', @import ); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub require { | ||||||||||||||||||||||||||
|
Subroutine
require: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ( @_ > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | Carp::confess("require() does not take arguments"); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 0 | $self->_load('require'); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 1 | ▶ | sub _load { | ||||||||||||||||||||||||||
|
Subroutine
_load: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||
| 168 | 1 | my ( $self, $use_or_require, @import ) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 1 | my $package = $self->package; | |||||||||||||||||||||||||||
| 171 | 1 | ▶ | if ( my $loaded = $self->is_package_loaded ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | Carp::confess("You have already loaded '$package' via '$loaded'"); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | my $caller = caller(1); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | my $import = ''; | |||||||||||||||||||||||||||
| 178 | 1 | ▶ | if (@import) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | require Data::Dumper; | |||||||||||||||||||||||||||
| 180 | 2 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 1 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 182 | 1 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 183 | 1 | $import = join ', ' => Data::Dumper::Dumper(@import); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | ▶ | my $import_list = $self->import_on_use ? $import : '()'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 1 | ▶ | eval <<" USE"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | package $caller; | ||||||||||||||||||||||||||||
| 189 | $use_or_require $package $import_list; | ||||||||||||||||||||||||||||
| 190 | USE | ||||||||||||||||||||||||||||
| 191 | 1 | ▶ | Carp::confess($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | my $to_install = $self->subs_installed; | |||||||||||||||||||||||||||
| 194 | 1 | foreach my $sub (keys %$to_install) { | |||||||||||||||||||||||||||
| 195 | # XXX we were going to do nifty checks to see if you could 'install' | ||||||||||||||||||||||||||||
| 196 | # or 'replace' a sub, but merely the existance of | ||||||||||||||||||||||||||||
| 197 | # Some::Package::subname() in another package would create the stash | ||||||||||||||||||||||||||||
| 198 | # slot. We're taking the easy way out. | ||||||||||||||||||||||||||||
| 199 | # my $stash = do { no strict 'refs'; \%{"${package}::"} }; | ||||||||||||||||||||||||||||
| 200 | # if (exists $stash->{$sub} ) {} | ||||||||||||||||||||||||||||
| 201 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | 3 | *{"${package}::$sub"} = $to_install->{$sub}; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1 | ▶ | unless ( $self->import_on_use ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 1 | eval <<" IMPORT"; | |||||||||||||||||||||||||||
| 208 | package $caller; | ||||||||||||||||||||||||||||
| 209 | $package->import($import); | ||||||||||||||||||||||||||||
| 210 | IMPORT | ||||||||||||||||||||||||||||
| 211 | 1 | ▶ | Carp::confess($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 1 | $self->_is_package_loaded($use_or_require); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 2 | 1; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||