| 1 | package Kelp::Base; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 56 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 56 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 56 | ▶ | use feature (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 56 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 56 | require namespace::autoclean; | |||||||||||||||||||||||||||
| 9 | 56 | require Kelp::Util; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub import | ||||||||||||||||||||||||||||
| 12 | 870 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 13 | 870 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 870 | my $caller = caller; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Do not import into inherited classes | ||||||||||||||||||||||||||||
| 17 | 870 | ▶ | return if $class ne __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 703 | ▶ | my $base = shift || $class; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $class
MC/DC0%0/2
shift() || $class
shift()
$class
Truth table66%2/3
A || B
A= shift()B= $class
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 703 | ▶ | if ($base ne '-strict') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 56 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 56 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 621 | ▶ | if ($base ne '-attr') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 620 | Kelp::Util::load_package($base); | |||||||||||||||||||||||||||
| 27 | 620 | push @{"${caller}::ISA"}, $base; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 621 | ▶ | *{"${caller}::attr"} = sub { attr($caller, @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 621 | namespace::autoclean->import( | |||||||||||||||||||||||||||
| 33 | -cleanee => $caller | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 703 | strict->import; | |||||||||||||||||||||||||||
| 38 | 703 | warnings->import; | |||||||||||||||||||||||||||
| 39 | 703 | feature->import(':5.10'); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub new | ||||||||||||||||||||||||||||
| 43 | 1252 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 1252 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | 1252 | return bless {@_}, $self; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub attr | ||||||||||||||||||||||||||||
| 49 | 2683 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
attr: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 2683 | my ($class, $name, $default) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2683 | ▶ | if (ref $default && ref $default ne 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $default and ref $default ne "CODE"
MC/DC0%0/2
ref $default and ref $default ne "CODE"
ref $default
ref $default ne "CODE"
Truth table66%2/3
A and B
A= ref $defaultB= ref $default ne "CODE"
| |||||||||||||||||||||||||||||
| 53 | 0 | croak "Default value for '$name' can not be a reference."; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Readonly attributes are marked with '-' | ||||||||||||||||||||||||||||
| 57 | 2683 | my $readonly = $name =~ s/^\-//; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Remember if default is a function | ||||||||||||||||||||||||||||
| 60 | 2683 | my $default_sub = ref $default eq 'CODE'; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 56 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 56 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2683 | ▶ | *{"${class}::$name"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 67 | 30629 | ▶ | return $_[0]->{$name} = $_[1] if @_ > 1 && !$readonly; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ > 1 and not $readonly
MC/DC100%2/2
@_ > 1 and not $readonly
@_ > 1
not $readonly
Truth table100%3/3
A and not(B)
A= @_ > 1B= not $readonly
| |||||||||||||||||||||||||||||
| 68 | 26722 | ▶ | return $_[0]->{$name} if exists $_[0]->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3242 | ▶ | return $_[0]->{$name} = $default_sub ? $default->($_[0]) : $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2683 | }; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 56 | 1; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 NAME | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Kelp::Base - Simple lazy attributes | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | use Kelp::Base; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | attr source => 'dbi:mysql:users'; | ||||||||||||||||||||||||||||
| 89 | attr user => 'test'; | ||||||||||||||||||||||||||||
| 90 | attr pass => 'secret'; | ||||||||||||||||||||||||||||
| 91 | attr opts => sub { { PrintError => 1, RaiseError => 1 } }; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | attr dbh => sub { | ||||||||||||||||||||||||||||
| 94 | my $self = shift; | ||||||||||||||||||||||||||||
| 95 | DBI->connect( $self->sourse, $self->user, $self->pass, $self->opts ); | ||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Later ... | ||||||||||||||||||||||||||||
| 99 | sub do_stuff { | ||||||||||||||||||||||||||||
| 100 | my $self = shift; | ||||||||||||||||||||||||||||
| 101 | $self->dbh->do('DELETE FROM accounts'); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | or | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | use Kelp::Base 'Module::Name'; # Extend Module::Name | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | or | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | use Kelp::Base -strict; # Only use strict, warnings and v5.10 | ||||||||||||||||||||||||||||
| 111 | # No magic | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | This module provides simple lazy attributes. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 WHY? | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Some users will naturally want to ask F<"Why not use Moose/Mouse/Moo/Mo?">. The | ||||||||||||||||||||||||||||
| 120 | answer is that the Kelp web framework needs lazy attributes, but the author | ||||||||||||||||||||||||||||
| 121 | wanted to keep the code light and object manager agnostic. This allows the | ||||||||||||||||||||||||||||
| 122 | users of the framework to choose an object manager to their liking. As a nice | ||||||||||||||||||||||||||||
| 123 | addition, our getters and constructors are quite a bit faster than any non-XS | ||||||||||||||||||||||||||||
| 124 | variant of L<Moose>, which makes the core code very fast. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | There is nothing more annoying than a module that forces you to use L<Moose> | ||||||||||||||||||||||||||||
| 127 | when you are perfectly fine with L<Moo> or L<Mo>, for example. Since this | ||||||||||||||||||||||||||||
| 128 | module is so minimal, you should probably switch to a full-blown OO system of | ||||||||||||||||||||||||||||
| 129 | your choice when writing your application. Kelp::Base should be compatible with | ||||||||||||||||||||||||||||
| 130 | it as long as it uses blessed hashes under the hood. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 USAGE | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | use Kelp::Base; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | The above will automatically include C<strict>, C<warnings> and C<v5.10>. It will | ||||||||||||||||||||||||||||
| 137 | also inject a new sub in the current class called C<attr>. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | attr name1 => 1; # Fixed value | ||||||||||||||||||||||||||||
| 140 | attr name2 => sub { [ 1, 2, 3 ] }; # Array | ||||||||||||||||||||||||||||
| 141 | attr name3 => sub { | ||||||||||||||||||||||||||||
| 142 | $_[0]->other; | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | ... | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | say $self->name1; # 1 | ||||||||||||||||||||||||||||
| 148 | $self->name2( [ 6, 7, 8 ] ); # Set new value | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | All those attributes will be available for reading and writing in each instance | ||||||||||||||||||||||||||||
| 151 | of the current class. If you want to create a read-only attribute, prefix its | ||||||||||||||||||||||||||||
| 152 | name with a dash. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | attr -readonly => "something"; | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Later | ||||||||||||||||||||||||||||
| 157 | say $self->readonly; # something | ||||||||||||||||||||||||||||
| 158 | $self->readonly("nothing"); # no change | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Kelp::Base can also be imported without turning an object into a class: | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # imports strict, warnings and :5.10 | ||||||||||||||||||||||||||||
| 163 | use Kelp::Base -strict; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # imports all of the above plus attr | ||||||||||||||||||||||||||||
| 166 | use Kelp::Base -attr; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | The former is useful for less boilerplate in scripts on older perls. The latter | ||||||||||||||||||||||||||||
| 169 | is useful when using C<attr> with L<Role::Tiny>. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | L<Kelp>, L<Moose>, L<Moo>, L<Mo>, L<Any::Moose> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||