| 1 | package Class::Tiny::Chained; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Class::Tiny (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | our @ISA = 'Class::Tiny'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.004'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | sub __gen_sub_body { | ||||||||||||||||||||||||||||||
|
Subroutine
__gen_sub_body: called CC=4 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 11 | 3 | my ($self, $name, $has_default, $default_type) = @_; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | if ($has_default && $default_type eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$has_default and $default_type eq "CODE"
MC/DC0%0/2
$has_default and $default_type eq "CODE"
$has_default
$default_type eq "CODE"
Truth table33%1/3
A and B
A= $has_defaultB= $default_type eq "CODE"
| |||||||||||||||||||||||||||||||||
| 14 | 0 | return << "HERE"; | |||||||||||||||||||||||||||||||
| 15 | sub $name { | ||||||||||||||||||||||||||||||||
| 16 | return ( | ||||||||||||||||||||||||||||||||
| 17 | ( \@_ == 1 ) | ||||||||||||||||||||||||||||||||
| 18 | ? ( exists \$_[0]{$name} ? \$_[0]{$name} : ( \$_[0]{$name} = \$default->( \$_[0] ) ) ) | ||||||||||||||||||||||||||||||||
| 19 | : scalar( \$_[0]{$name} = \$_[1], \$_[0] ) | ||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||
| 22 | HERE | ||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | elsif ($has_default) { | ||||||||||||||||||||||||||||||||
| 25 | 0 | return << "HERE"; | |||||||||||||||||||||||||||||||
| 26 | sub $name { | ||||||||||||||||||||||||||||||||
| 27 | return ( | ||||||||||||||||||||||||||||||||
| 28 | ( \@_ == 1 ) | ||||||||||||||||||||||||||||||||
| 29 | ? ( exists \$_[0]{$name} ? \$_[0]{$name} : ( \$_[0]{$name} = \$default ) ) | ||||||||||||||||||||||||||||||||
| 30 | : scalar( \$_[0]{$name} = \$_[1], \$_[0] ) | ||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | HERE | ||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||||||
| 36 | 3 | return << "HERE"; | |||||||||||||||||||||||||||||||
| 37 | sub $name { | ||||||||||||||||||||||||||||||||
| 38 | return \@_ == 1 ? \$_[0]{$name} : scalar( \$_[0]{$name} = \$_[1], \$_[0] ) | ||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | HERE | ||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 1 | 1; | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | Class::Tiny::Chained - Minimalist class construction, with chained attributes | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | In I<Person.pm>: | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | package Person; | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | use Class::Tiny::Chained qw( name ); | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 1; | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | In I<Employee.pm>: | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | package Employee; | ||||||||||||||||||||||||||||||||
| 63 | use parent 'Person'; | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | use Class::Tiny::Chained qw( ssn ), { | ||||||||||||||||||||||||||||||||
| 66 | timestamp => sub { time } # attribute with default | ||||||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 1; | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | In I<example.pl>: | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | use Employee; | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | my $obj = Employee->new( name => "Larry", ssn => "111-22-3333" ); | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | # attribute setters are chainable | ||||||||||||||||||||||||||||||||
| 78 | my $obj = Employee->new->name("Fred")->ssn("444-55-6666"); | ||||||||||||||||||||||||||||||||
| 79 | my $ts = $obj->name("Bob")->timestamp; | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | L<Class::Tiny::Chained> is a wrapper around L<Class::Tiny> which makes the | ||||||||||||||||||||||||||||||||
| 84 | generated attribute accessors chainable; that is, when setting an attribute | ||||||||||||||||||||||||||||||||
| 85 | value, the object is returned so that further methods can be called. | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | Dan Book <dbook@cpan.org> | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | This software is Copyright (c) 2015 by Dan Book. | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | L<Object::Tap>, L<MooX::ChainedAttributes> | ||||||||||||||||||||||||||||||||