| 1 | package Class::Accessor::Lite::Lazy; | ||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use 5.008_001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use parent 'Class::Accessor::Lite'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 3 | our $VERSION = '0.03'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 3 | my %key_ctor = ( | |||||||||||||
| 11 | ro_lazy => \&_mk_ro_lazy_accessors, | ||||||||||||||
| 12 | rw_lazy => \&_mk_lazy_accessors, | ||||||||||||||
| 13 | ); | ||||||||||||||
| 14 | |||||||||||||||
| 15 | 4 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||||||
| 16 | 4 | my ($class, %args) = @_; | |||||||||||||
| 17 | 4 | my $pkg = caller; | |||||||||||||
| 18 | 4 | foreach my $key (sort keys %key_ctor) { | |||||||||||||
| 19 | 8 | ▶ | if (defined (my $value = delete $args{$key})) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | 3 | ▶ | Carp::croak "value of the '$key' parameter should be an arrayref or hashref" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | unless ref($value) =~ /^(ARRAY|HASH)$/; | ||||||||||||||
| 22 | 3 | ▶ | $value = [ $value ] if ref $value eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 3 | $key_ctor{$key}->($pkg, @$value); | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | } | ||||||||||||||
| 26 | 4 | @_ = ($class, %args); | |||||||||||||
| 27 | 4 | goto \&Class::Accessor::Lite::import; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 1 | ▶ | sub mk_lazy_accessors { | ||||||||||||
|
Subroutine
mk_lazy_accessors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 31 | 1 | (undef, my @properties) = @_; | |||||||||||||
| 32 | 1 | my $pkg = caller; | |||||||||||||
| 33 | 1 | _mk_lazy_accessors($pkg, @properties); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | ▶ | sub mk_ro_lazy_accessors { | ||||||||||||
|
Subroutine
mk_ro_lazy_accessors: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 37 | 0 | (undef, my @properties) = @_; | |||||||||||||
| 38 | 0 | my $pkg = caller; | |||||||||||||
| 39 | 0 | _mk_ro_lazy_accessors($pkg, @properties); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 2 | ▶ | sub _mk_ro_lazy_accessors { | ||||||||||||
|
Subroutine
_mk_ro_lazy_accessors: called CC=3 SCAR=11.0
| |||||||||||||||
| 43 | 2 | my $pkg = shift; | |||||||||||||
| 44 | 2 | ▶ | my %decls = map { ref $_ eq 'HASH' ? ( %$_ ) : ( $_ => undef ) } @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 3 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 2 | ▶ | while (my ($name, $builder) = each %decls) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 4 | *{"$pkg\::$name"} = __m_ro_lazy($pkg, $name, $builder); | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | ▶ | sub _mk_lazy_accessors { | ||||||||||||
|
Subroutine
_mk_lazy_accessors: called CC=3 SCAR=11.0
| |||||||||||||||
| 52 | 2 | my $pkg = shift; | |||||||||||||
| 53 | 2 | ▶ | my %decls = map { ref $_ eq 'HASH' ? ( %$_ ) : ( $_ => undef ) } @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 3 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 55 | 2 | ▶ | while (my ($name, $builder) = each %decls) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 4 | *{"$pkg\::$name"} = __m_lazy($name, $builder); | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 4 | ▶ | sub __m_ro_lazy { | ||||||||||||
|
Subroutine
__m_ro_lazy: called CC=2 SCAR=7.1
| |||||||||||||||
| 61 | 4 | my ($pkg, $name, $builder) = @_; | |||||||||||||
| 62 | 4 | ▶ | $builder = "_build_$name" unless defined $builder; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 5 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||
| 64 | 5 | ▶ | if (@_ == 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 5 | ▶ | return $_[0]->{$name} if exists $_[0]->{$name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 4 | return $_[0]->{$name} = $_[0]->$builder; | |||||||||||||
| 67 | } else { | ||||||||||||||
| 68 | 0 | my $caller = caller(0); | |||||||||||||
| 69 | 0 | Carp::croak("'$caller' cannot access the value of '$name' on objects of class '$pkg'"); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | 4 | }; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 4 | ▶ | sub __m_lazy { | ||||||||||||
|
Subroutine
__m_lazy: called CC=2 SCAR=7.0
| |||||||||||||||
| 75 | 4 | my ($name, $builder) = @_; | |||||||||||||
| 76 | 4 | ▶ | $builder = "_build_$name" unless defined $builder; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 10 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||
| 78 | 10 | ▶ | if (@_ == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 79 | 8 | ▶ | return $_[0]->{$name} if exists $_[0]->{$name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 4 | return $_[0]->{$name} = $_[0]->$builder; | |||||||||||||
| 81 | } elsif (@_ == 2) { | ||||||||||||||
| 82 | 2 | return $_[0]->{$name} = $_[1]; | |||||||||||||
| 83 | } else { | ||||||||||||||
| 84 | 0 | return shift->{$name} = \@_; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | 4 | }; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 3 | 1; | |||||||||||||
| 90 | |||||||||||||||
| 91 | __END__ | ||||||||||||||