| 1 | package Eve::Class; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 30 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 30 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 30 | ▶ | use Contextual::Return; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 30 | ▶ | use Eve::Support; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 30 | our $AUTOLOAD; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | B<Eve::Class> - a class that all other library classes use as a | ||||||||||||||||||||
| 15 | parent. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | use parent qw(Eve::Class); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | sub init { | ||||||||||||||||||||
| 22 | my ($self, %arg_hash) = @_; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | $self->{'_private_property'} = 'Private value'; | ||||||||||||||||||||
| 25 | $self->{'public_property'} = 'Public value'; | ||||||||||||||||||||
| 26 | $self->_private_method(); | ||||||||||||||||||||
| 27 | $self->public_method(); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | return; | ||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | B<Eve::Class> is an abstract class whose functionality is used in | ||||||||||||||||||||
| 35 | derived classes in order to avoid initialization code duplication and | ||||||||||||||||||||
| 36 | make routine procedures easier. | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =head2 Implicit accessors | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | Another purpose of this class is to provide a mechanism that | ||||||||||||||||||||
| 41 | simplifies access to object properties. | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | Every property can be accessed using C<$foo->name> notation. If there | ||||||||||||||||||||
| 44 | is additional processing required before getting or setting a property | ||||||||||||||||||||
| 45 | custom getters and setters can be used. They can be defined as methods | ||||||||||||||||||||
| 46 | by prefixing the property name with 'get_' or 'set_'. In this case | ||||||||||||||||||||
| 47 | they will be called instead of accessing the property directly. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | package Foo; | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | use parent qw(Eve::Class); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | sub init { | ||||||||||||||||||||
| 54 | my $self = shift; | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | $self->{'name'} = 'some value'; | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | return; | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | sub set_name { | ||||||||||||||||||||
| 62 | my ($self, $value) = @_; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | $self->{'name'} = lc($value); | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1; | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # Later ... | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | $foo->name = 'Another Value'; | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head1 METHODS | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head2 B<new()> | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | This method is the constructor. It can be called both on the class | ||||||||||||||||||||
| 78 | and on the object. Calling the constructor on an existing object | ||||||||||||||||||||
| 79 | will return a new object of the instance's class. | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | package Foo; | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | use parent qw(Eve::Class); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 1; | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # Later ... | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | my $foo1 = Foo->new('Your arguments here'); | ||||||||||||||||||||
| 90 | my $foo2 = $foo1->new('Your new arguments here'); | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head3 Arguments | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | An arbitrary number of arguments which will be later passed to the | ||||||||||||||||||||
| 95 | C<init()> method. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head3 Returns | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | A new instance of the class that the method is being called on. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 142 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 104 | 142 | my $class = shift; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 142 | my $self = {}; | |||||||||||||||||||
| 107 | 142 | ▶ | bless($self, (ref($class) or $class)); | ||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | # Calling init with the same @_ | ||||||||||||||||||||
| 110 | 142 | $self->init(@_); | |||||||||||||||||||
| 111 | 139 | return $self; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head2 B<init()> | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | This method is called after an object has been instantiated using the C<new()> | ||||||||||||||||||||
| 117 | method, all parameters that have been passed to the constructor are passed | ||||||||||||||||||||
| 118 | to this method also. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | If you need the object to have certain properties, you need to explicitly | ||||||||||||||||||||
| 121 | define them as hash keys inside this method. | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | package Foo; | ||||||||||||||||||||
| 124 | use parent qw(Eve::Class); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | sub init { | ||||||||||||||||||||
| 127 | my ($self, %arg_hash) = @_; | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | $self->{'_private_property'} = 'Private value'; | ||||||||||||||||||||
| 130 | $self->{'public_property'} = 'Public value'; | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | return; | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 1; | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | After that you can access the object's properties in this manner: | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | my $foo1 = Foo->new(); | ||||||||||||||||||||
| 140 | print $foo1->public_property; | ||||||||||||||||||||
| 141 | $foo1->public_property = 'Another value'; | ||||||||||||||||||||
| 142 | $foo1->_private_property = 'Whoops!'; | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Please note that there is no mechanism to make properties really private or | ||||||||||||||||||||
| 145 | protected, which means that you can access all properties from outside, no | ||||||||||||||||||||
| 146 | matter what they are called. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head3 Arguments | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Note that this method should not be called directly. The C<new()> method | ||||||||||||||||||||
| 151 | must be called instead, and all its arguments will be passed directly to | ||||||||||||||||||||
| 152 | this method. | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 4 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 157 | # Init method stub | ||||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 944 | ▶ | sub _accessor { | ||||||||||||||||||
|
Subroutine
_accessor: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 161 | 944 | my ($self, %arg_hash) = @_; | |||||||||||||||||||
| 162 | 944 | Eve::Support::arguments(\%arg_hash, my ($is_set, $name, $value)); | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 944 | ▶ | if (not exists $self->{$name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 6 | Eve::Error::Attribute->throw( | |||||||||||||||||||
| 166 | message => "Property $name does not exist"); | ||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 938 | my $method = ('_get', '_set')[$is_set].'_'.$name; | |||||||||||||||||||
| 170 | 938 | my $result; | |||||||||||||||||||
| 171 | 938 | ▶ | if ($self->can($method)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 159 | $result = $self->$method($value); | |||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||
| 174 | 779 | ▶ | if ($is_set) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 6 | $self->{$name} = $value; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | 779 | $result = $self->{$name} | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 938 | return $result; | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 560 | ▶ | sub AUTOLOAD : lvalue { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 184 | 560 | my $self = shift; | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 560 | my $name = $AUTOLOAD; | |||||||||||||||||||
| 187 | 560 | $name =~ s/.*:://; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 4 | ▶ | NVALUE { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 190 | 4 | Eve::Error::Attribute->throw( | |||||||||||||||||||
| 191 | message => "Method $name does not exist") } | ||||||||||||||||||||
| 192 | 52 | ▶ | LVALUE { $self->_accessor(is_set => 1, name => $name, value => $_) } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 193 | 892 | ▶ | RVALUE { $self->_accessor(is_set => 0, name => $name, value => undef) } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 194 | 560 | } | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 197 | 0 | return; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | Copyright 2012 Igor Zinovyev. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 205 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 206 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =over 4 | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =back | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 30 | 1; | |||||||||||||||||||