blib/lib/Attribute/Property.pm

filter: Statementstmt 53.5% 38 / 71 Branchbran 20.5% 7 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 52.6% 10 / 19 Podpod 0.0% 0 / 2 totaltotal 41.9% 55 / 131 CC 19 SCAR 30.0
CC
19
Cov
42%
__ANON__
56.0
__ANON__
42.0
__ANON__
12.0
CRAP 20.0
↑ Summary
1 package Attribute::Property;
2  
3 # $Id: Property.pm,v 1.48 2003/04/21 16:04:14 juerd Exp $
4  
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # use Want qw(want rreturn);
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
11 1 ▶ if (eval { require Want }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Want } }) { } 0 1 1
12 0 *want = *Want::want;
13 0 *rreturn = *Want::rreturn;
14 } else {
15 0 ▶ *want = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 0 ▶ *rreturn = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 }
18 }
19  
20 1 ▶no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our $VERSION = '1.05';
24  
25 1 $Carp::Internal{Attribute::Handlers}++; # may we be forgiven for our sins
26 1 $Carp::Internal{+__PACKAGE__}++;
27  
28 1 my %p;
29  
30 13 ▶sub UNIVERSAL::Property : ATTR(CODE) {
Subroutine Property: called CC=3 SCAR=18.8
Pod undocumented
31 13 my (undef, $s, $r) = @_;
32 13 ▶ croak "Cannot use Property attribute with anonymous sub" unless ref $s;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $s 1 12 13
33 12 my $n = *$s{NAME};
34 *$s = defined &$s
35 0 ▶ ? sub : lvalue {
Subroutine __ANON__: not called CC=6 SCAR=37.4
36 0 ▶ croak "Too many arguments for $n method" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 0 0
37 0 ▶ if (want 'RVALUE') {
Branch: 1 branch
BranchTrueFalseTotal
if (want("RVALUE")) 0 0 0
38 0 ▶ rreturn $_[0]{$n} if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
39 0 ▶ $r->($_[0], local $_ = $_[1])
Branch: 1 branch
BranchTrueFalseTotal
&$r($_[0], local $_ = $_[1]) or croak("Invalid value for $n property") 0 0 0
40 or croak "Invalid value for $n property";
41 0 rreturn $_[0]{$n} = $_;
42 }
43 0 tie my $foo, __PACKAGE__, ${ \$_[0]{$n} }, $r, $_[0], $n;
44 0 ▶ @_ == 2 ? ( $foo = $_[1] ) : $foo
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
45 }
46 0 ▶ : sub : lvalue {
Subroutine __ANON__: not called CC=3 SCAR=24.8
47 0 ▶ croak "Too many arguments for $n method" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 0 0
48 0 ▶ @_ == 2 ? ( $_[0]{$n} = $_[1] ) : ${ \$_[0]{$n} }
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
49 12 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined &$s ? : 11 1 12
50 12 undef $p{\&$s};
51 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 0 ▶sub TIESCALAR { bless \@_, shift } # @_ = (class, lvalue, subref, object, name)
Subroutine TIESCALAR: not called CC=1 SCAR=6.9
54 0 ▶sub FETCH { $_[0][0] }
Subroutine FETCH: not called CC=1 SCAR=6.9
55  
56 0 ▶sub STORE {
Subroutine STORE: not called CC=2 SCAR=17.9
57 0 ▶ $_[0][1]->($_[0][2], local $_ = $_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[0][1]->($_[0][2], local $_ = $_[1]) or croak("Invalid value for $_[0][3] property") 0 0 0
58 or croak "Invalid value for $_[0][3] property";
59 0 $_[0][0] = $_;
60 }
61  
62 3 ▶sub UNIVERSAL::New : ATTR(CODE) {
Subroutine New: called CC=2 SCAR=14.3
Pod undocumented
63 3 my ($P, $s, $r) = @_;
64 3 my $n = *$s{NAME};
65 3 ▶ undef $r if not defined &$s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$s 1 2 3
66 0 ▶ *$s = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
67 0 my $c = shift;
68 0 ▶ croak qq(Can't call method "$n" on a reference) if ref $c;
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 0 0 0
69 0 ▶ croak "Odd number of arguments for $c->$n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
70 0 my $o = bless {}, $c;
71 0 my $l = \&Carp::shortmess;
72 0 ▶ local *Carp::shortmess = sub { $_[-1] .= " in $c->$n"; &$l; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ while (my ($p, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
74 0 my $m = $o->can($p);
75 0 ▶ $m and exists $p{$m} or croak qq(No such property "$p");
Branch: 1 branch
BranchTrueFalseTotal
$m and exists $p{$m} or croak(qq[No such property "$p"]) 0 0 0
Condition0%0/3
$m and exists $p{$m}
!ll&&!rl&&r
000
MC/DC0%0/2
$m and exists $p{$m}
$m exists $p{$m}
Truth table0%0/3
A and B
A= $mB= exists $p{$m}
ABresult
0X0
100
111
76 0 $m->($o, $v);
77 }
78 0 ▶ return $r->($o) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
79 0 return $o;
80 3 };
81 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 1 1;
84  
85 =head1 NAME
86  
87 Attribute::Property - Easy lvalue accessors with validation. ($foo->bar = 42)
88  
89 =head1 SYNOPSIS
90  
91 =head2 CLASS
92  
93 use Attribute::Property;
94 use Carp;
95  
96 package SomeClass;
97  
98 sub new : New { further initialization here ... }
99
100 sub nondigits : Property { /^\D+\z/ }
101 sub digits : Property { /^\d+\z/ or croak "custom error message" }
102 sub anyvalue : Property;
103 sub another : Property;
104  
105 sub value : Property {
106 my $self = shift; # Object is accessible as $_[0]
107 s/^\s+//; # New value can be altered through $_ or $_[1]
108  
109 $_ <= $self->maximum or croak "Value exceeds maximum";
110 }
111  
112 package Person;
113  
114 sub new : New;
115 sub name : Property;
116 sub age : Property { /^\d+\z/ and $_ > 0 }
117  
118 =head2 USAGE
119  
120 my $object = SomeClass->new(digits => '123');
121  
122 $object->nondigits = "abc";
123 $object->digits = "123";
124 $object->anyvalue = "abc123\n";
125  
126 $object->anyvalue('archaic style still works');
127  
128 my $john = Person->new(name => 'John Doe', age => 19);
129
130 $john->age++;
131 printf "%s is now %d years old", $john->name, $john->age;
132  
133 # These would croak
134 $object->nondigits = "987";
135 $object->digits = "xyz";
136  
137 =head1 DESCRIPTION
138  
139 This module introduces two attributes that make object oriented programming
140 much easier. You can just define a constructor and some properties without
141 having to write accessors.
142  
143 =over 4
144  
145 =item C<Property>
146  
147 sub color : Property;
148 sub color : Property { /^#[0-9A-F]{6}$/ }
149  
150 The C<Property> attribute turns a method into an object property. The original
151 code block is used only to validate new values, the module croaks if it returns
152 false. The method returns an I<lvalue>, meaning that you can create a reference
153 to it, assign to it and apply a regex to it.
154  
155 Undefined subs (subs that have been declared but do not have a code block) with
156 the C<Property> attribute will be properties without any value validation.
157  
158 In the validation code block, the object is in C<$_[0]> and the value to be
159 validated is aliased as C<$_[1]> and for regexing convenience as C<$_>.
160  
161 Feel free to croak explicitly if you don't want the default error message.
162  
163 =item C<New>
164  
165 sub new : New;
166 sub new : New { my $self = shift; ...; return $self; }
167  
168 The C<New> attribute turns a method into an object constructor. The original
169 code block can be used for further initialization, but it is completely
170 optional.
171  
172 The constructor takes named arguments in C<< property => value >> pairs and
173 populates the hash with the given pairs. After validating them, of course.
174  
175 The new object is passed to the initialization code block as C<$_[0]>. Be
176 sure to return the object if you use any initialization block. If there is
177 no initialization code block, Attribute::Property takes care of returning
178 the new object.
179  
180 =back
181  
182 =head1 PREREQUISITES
183  
184 Your object must be a blessed hash reference. The property names will be used
185 for the hash keys.
186  
187 For class properties of C<Some::Module>, the hash C<%Some::Module> is used.
188 For class properties of packages without C<::>, the behaviour is undefined.
189  
190 In short: C<< $foo->bar = 14 >> and C<< $foo->bar(14) >> assign 14 to
191 C<< $foo->{bar} >> after positive validation. The same thing happens with C<< my
192 $foo = Class->new(bar => 14); >> given that C<Class::new> uses the C<New>
193 property.
194  
195 If you have the Want module installed, Attribute::Property will use it to make
196 rvalue method calls more efficient.
197  
198 =head1 COMPATIBILITY
199  
200 Old fashioned C<< $object->property(VALUE) >> is still available.
201  
202 This module requires a modern Perl (5.6.0+), fossils like Perl 5.00x don't
203 support our chicanery.
204  
205 =head1 BUGS
206  
207 =over 2
208  
209 =item *
210  
211 The C<New> attribute should really be called C<Constructor>, but that would
212 conflict with the existing Attribute::Constructor module.
213  
214 =back
215  
216 =head1 LICENSE
217  
218 There is no license. This software was released into the public domain. Do
219 with it what you want, but on your own risk. Both authors disclaim any
220 responsibility.
221  
222 =head1 AUTHORS
223  
224 Juerd Waalboer <juerd@cpan.org> <http://juerd.nl/>
225  
226 Matthijs van Duin <xmath@cpan.org>
227  
228 =cut
229  
230 # vim: ft=perl sts=0 noet sw=8 ts=8
231  
↑ Summary