| 1 | package Data::All::Base; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | |||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | ▶ | use Symbol; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | use base 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | our @EXPORT = qw(new internal attribute populate error ); | |||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | our $VERSION = 1.0; | |||||||||||||
| 14 | |||||||||||||||
| 15 | sub internal; | ||||||||||||||
| 16 | 1 | internal 'ERROR' => []; | |||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | ▶ | sub error { | ||||||||||||
|
Subroutine
error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 19 | 0 | my $self = shift; | |||||||||||||
| 20 | 0 | ▶ | push (@{ $self->__ERROR() }, @_) if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | 0 | $self->__ERROR(); | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | sub new | ||||||||||||||
| 25 | # Bypass Spiffy's new, so we can call init() | ||||||||||||||
| 26 | 9 | ▶ | { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||
| 27 | 9 | my $class = shift; | |||||||||||||
| 28 | 9 | my $self = bless Symbol::gensym(), $class; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 9 | ▶ | return ($self->can('init')) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | ? $self->init(@_) | ||||||||||||||
| 32 | : $self; | ||||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | |||||||||||||||
| 36 | sub attribute | ||||||||||||||
| 37 | # Creates an anonymous subroutine and places it in the caller's | ||||||||||||||
| 38 | # package (i.e. $self->name). | ||||||||||||||
| 39 | # Consider lvalue expression to allow $self->name = "newvalue". | ||||||||||||||
| 40 | # http://perl.active-venture.com/pod/perlsub.html | ||||||||||||||
| 41 | 18 | ▶ | { | ||||||||||||
|
Subroutine
attribute: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 42 | 18 | my $package = caller; | |||||||||||||
| 43 | 18 | my ($attribute, $default) = @_; | |||||||||||||
| 44 | 1 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | 18 | ▶ | return if defined &{"${package}::$attribute"}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 18 | *{"${package}::$attribute"} = | |||||||||||||
| 47 | 156 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||
| 48 | 156 | my $self = shift; | |||||||||||||
| 49 | 156 | ▶ | unless (exists *$self->{$attribute}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 40 | ▶ | *$self->{$attribute} = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 51 | ref($default) eq 'ARRAY' ? [] : | ||||||||||||||
| 52 | ref($default) eq 'HASH' ? {} : | ||||||||||||||
| 53 | $default; | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | 156 | ▶ | return *$self->{$attribute} unless @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 47 | *$self->{$attribute} = shift; | |||||||||||||
| 57 | 18 | }; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | sub internal | ||||||||||||||
| 61 | # Used like attribute 'name' => 'val'. The difference being | ||||||||||||||
| 62 | # the internal attribute and it accessor are stored as '__name' | ||||||||||||||
| 63 | 10 | ▶ | { | ||||||||||||
|
Subroutine
internal: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 64 | 10 | my $package = caller; | |||||||||||||
| 65 | 10 | my ($attribute, $default) = @_; | |||||||||||||
| 66 | 10 | $attribute = "__$attribute"; | |||||||||||||
| 67 | 1 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 68 | 10 | ▶ | return if defined &{"${package}::$attribute"}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 10 | *{"${package}::$attribute"} = | |||||||||||||
| 70 | 105 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 71 | 105 | my $self = shift; | |||||||||||||
| 72 | 105 | ▶ | unless (exists *$self->{$attribute}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 18 | *$self->{$attribute} = $default; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | 105 | ▶ | return *$self->{$attribute} unless @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 20 | *$self->{$attribute} = shift; | |||||||||||||
| 77 | 10 | }; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | |||||||||||||||
| 81 | sub populate | ||||||||||||||
| 82 | # populate $self->ACCESSOR with arguments in $args. | ||||||||||||||
| 83 | # This is usually called by init() after the args have been parsed. | ||||||||||||||
| 84 | 10 | ▶ | { | ||||||||||||
|
Subroutine
populate: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||
| 85 | 10 | my ($self, $args) = @_; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 10 | for my $a (keys %{ $args }) | |||||||||||||
| 88 | { | ||||||||||||||
| 89 | 36 | ▶ | warn("No attribute method for $a"), next | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | unless $self->can($a); | ||||||||||||||
| 91 | #warn 9, "Running $a"; | ||||||||||||||
| 92 | 36 | $self->$a($args->{$a}); | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | |||||||||||||||
| 97 | |||||||||||||||
| 98 | |||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | 1 | 1; | |||||||||||||