| 1 | package Blosxom::Plugin; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use 5.008_009; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | our $VERSION = '0.02004'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 10 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 11 | 2 | my $component = scalar caller; | |||||||||||||||||||||||||||
| 12 | 9 | ▶ | my $stash = do { no strict 'refs'; \%{"$component\::"} }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | my %is_excluded; | |||||||||||||||||||||||||||
| 15 | 2 | ▶ | while ( my ($method, $glob) = each %{$stash} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 274 | ▶ | next unless defined *{$glob}{CODE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 28 | $is_excluded{$method}++; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | my ( @requires, @accessors ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my @exports = ( | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | requires => sub { shift; push @requires, @_ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | mk_accessors => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 25 | 1 | my $pkg = shift; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | my @args = ref $_[0] eq 'HASH' ? %{$_[0]} : map { $_ => undef } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 1 | push @accessors, @args, | |||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | init => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 30 | 2 | my ( $comp, $plugin ) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | if ( my @methods = grep { !$plugin->can($_) } @requires ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | my $methods = join ', ', @methods; | |||||||||||||||||||||||||||
| 34 | 1 | croak "Can't apply '$comp' to '$plugin' - missing $methods"; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | for ( my $i = 0; $i < @accessors; $i += 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 2 | $plugin->add_attribute( @accessors[$i, $i+1] ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | while ( my ($method, $glob) = each %{$stash} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 7 | ▶ | if ( my $code = *{$glob}{CODE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 5 | ▶ | next if $is_excluded{$method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | $plugin->add_method( $method => $code ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | return; | |||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | 2 | ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # export mixin methods | ||||||||||||||||||||||||||||
| 53 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | while ( my ( $method, $code ) = splice @exports, 0, 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 6 | *{ "$component\::$method" } = $code; | |||||||||||||||||||||||||||
| 56 | 6 | $is_excluded{ $method }++; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | return; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | my %attribute_of; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | sub mk_accessors { | ||||||||||||||||||||||||||
|
Subroutine
mk_accessors: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 2 | my $package = shift; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | my @accessors = ref $_[0] eq 'HASH' ? %{ $_[0] } : map { $_ => undef } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | while ( my ($field, $default) = splice @accessors, 0, 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 6 | *{"$package\::$field"} = $package->make_accessor($field, $default); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | return; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 11 | ▶ | sub make_accessor { | ||||||||||||||||||||||||||
|
Subroutine
make_accessor: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 11 | my $package = shift; | |||||||||||||||||||||||||||
| 78 | 11 | my $name = shift; | |||||||||||||||||||||||||||
| 79 | 11 | my $default = shift; | |||||||||||||||||||||||||||
| 80 | 11 | ▶ | my $attribute = $attribute_of{$package} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$attribute_of{$package} ||= {}
MC/DC100%1/1
$attribute_of{$package} ||= {}
$attribute_of{$package}
Truth table100%2/2
A
A= $attribute_of{$package}
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 11 | ▶ | if ( ref $default eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | return $attribute->{$name} = $_[1] if @_ == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | return $attribute->{$name} if exists $attribute->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | return $attribute->{$name} = $package->$default; | |||||||||||||||||||||||||||
| 87 | 4 | }; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | elsif ( defined $default ) { | ||||||||||||||||||||||||||||
| 90 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 91 | 7 | ▶ | return $attribute->{$name} = $_[1] if @_ == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 5 | ▶ | return $attribute->{$name} if exists $attribute->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 5 | return $attribute->{$name} = $default; | |||||||||||||||||||||||||||
| 94 | 6 | }; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 2 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | @_ > 1 ? $attribute->{$name} = $_[1] : $attribute->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | }; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub end { %{ $attribute_of{$_[0]} } = () if exists $attribute_of{$_[0]} } | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 4 | my $package = shift; | |||||||||||||||||||||||||||
| 109 | 4 | require Data::Dumper; | |||||||||||||||||||||||||||
| 110 | 4 | ▶ | local $Data::Dumper::Maxdepth = shift || 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 111 | 4 | Data::Dumper::Dumper( $attribute_of{$package} ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | ▶ | sub component_base_class { __PACKAGE__ } | ||||||||||||||||||||||||||
|
Subroutine
component_base_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 9 | ▶ | sub load_components { | ||||||||||||||||||||||||||
|
Subroutine
load_components: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 9 | my $package = shift; | |||||||||||||||||||||||||||
| 118 | 9 | my @args = @_; | |||||||||||||||||||||||||||
| 119 | 9 | my $prefix = $package->component_base_class; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 9 | my ( $component, %is_loaded, %has_conflict, %code_of ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | local *add_component | ||||||||||||||||||||||||||||
| 124 | 9 | ▶ | = sub { push @args, @_ > 2 ? @_[1, 2] : $_[1] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 16 | ▶ | local *add_method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 127 | 16 | my ( $pkg, $method, $code ) = @_; | |||||||||||||||||||||||||||
| 128 | 16 | ▶ | unless ( defined &{"$package\::$method"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 14 | ▶ | push @{ $has_conflict{$method} ||= [] }, $component; | ||||||||||||||||||||||||||
|
Condition100%2/2
$has_conflict{$method} ||= []
MC/DC100%1/1
$has_conflict{$method} ||= []
$has_conflict{$method}
Truth table100%2/2
A
A= $has_conflict{$method}
| |||||||||||||||||||||||||||||
| 130 | 14 | $code_of{ $method } = $code; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 9 | }; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 9 | ▶ | while ( @args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 17 | $component = do { | |||||||||||||||||||||||||||
| 136 | 17 | my $class = shift @args; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 17 | ▶ | if ( $class !~ s/^\+// and $class !~ /^$prefix/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class =~ s/^\+// or $class =~ /^$prefix/
MC/DC50%1/2
$class =~ s/^\+// or $class =~ /^$prefix/
$class =~ s/^\+//
$class =~ /^$prefix/
Truth table66%2/3
A or B
A= $class =~ s/^\+//B= $class =~ /^$prefix/
| |||||||||||||||||||||||||||||
| 139 | 3 | $class = "$prefix\::$class"; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 17 | ▶ | if ( $is_loaded{$class}++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | shift @args if ref $args[0] eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 2 | next; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 15 | ( my $file = $class ) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 148 | 15 | require "$file.pm"; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 15 | $class; | |||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 15 | ▶ | my $config = ref $args[0] eq 'HASH' ? shift @args : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 15 | $component->init( $package, $config ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 8 | ▶ | if ( %code_of ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 6 | ▶ | while ( my ( $method, $components ) = each %has_conflict ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 13 | ▶ | delete $has_conflict{ $method } if @{ $components } == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 13 | *{ "$package\::$method" } = $code_of{ $method }; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | ▶ | if ( %has_conflict ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | croak join "\n", map { | ||||||||||||||||||||||||||||
| 168 | 1 | "Due to a method name conflict between components " . | |||||||||||||||||||||||||||
| 169 | 1 | "'" . join( ' and ', sort @{ $has_conflict{$_} } ) . "', " . | |||||||||||||||||||||||||||
| 170 | "the method '$_' must be implemented by '$package'"; | ||||||||||||||||||||||||||||
| 171 | } keys %has_conflict; | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 7 | return; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 5 | ▶ | sub add_attribute { | ||||||||||||||||||||||||||
|
Subroutine
add_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 5 | my ( $pkg, $name, $default ) = @_; | |||||||||||||||||||||||||||
| 179 | 5 | $pkg->add_method( $name => $pkg->make_accessor($name, $default) ); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | sub has_method { defined &{"$_[0]::$_[1]"} } | ||||||||||||||||||||||||||
|
Subroutine
has_method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 9 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||