Gomor.pm

filter: Statementstmt 86.9% 80 / 92 Branchbran 72.7% 16 / 22 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.1% 16 / 17 Podpod 100% 8 / 8 totaltotal 81.2% 121 / 149 CC 20 SCAR 15.2
CC
20
Cov
81%
cgDebugPrint
12.0
cgCheckParams
8.4
cgGetAttributes
5.0
CRAP 4.6
↑ Summary Gomor/Array.pm »
1 #
2 # $Id: Gomor.pm 2000 2015-01-13 18:24:09Z gomor $
3 #
4 package Class::Gomor;
5 11 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '1.03';
8  
9 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 our $Debug = 0;
17 11 our $NoCheck = 0;
18 11 our @EXPORT_OK = qw($Debug $NoCheck);
19  
20 6 ▶sub cgCheckParams {
Subroutine cgCheckParams: called CC=7 SCAR=21.3
Pod documented
21 6 my $self = shift;
22 6 my ($userParams, $accessors) = @_;
23 6 for my $u (keys %$userParams) {
24 2 my $valid;
25 my $defined;
26 2 for (@$accessors) {
27 3 ▶ ($u eq $_) ? $valid++ : next;
Branch: 1 branch
BranchTrueFalseTotal
$u eq $_ ? : 2 1 3
28 2 ▶ defined($userParams->{$u}) && do { $defined++; last };
Branch: 1 branch
BranchTrueFalseTotal
defined $userParams->{$u} and do { ++$defined; last } 2 0 2
29 }
30 2 ▶ if (! $valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 0 2 2
31 0 carp("$self: parameter is invalid: `$u'");
32 0 next;
33 }
34 2 ▶ if (! $defined) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($defined) 0 2 2
35 0 carp("$self: parameter is undef: `$u'");
36 0 next;
37 }
38 }
39 }
40  
41 30 ▶sub cgGetIsaTree {
Subroutine cgGetIsaTree: called CC=3 SCAR=11.0
Pod documented
42 30 my $self = shift;
43 30 my ($classes) = @_;
44 30 for (@{$self.'::ISA'}) {
45 30 push @$classes, $_;
46 30 ▶ $_->cgGetIsaTree($classes) if $_->can('cgGetIsaTree');
Branch: 1 branch
BranchTrueFalseTotal
if $_->can("cgGetIsaTree") 20 10 30
47 }
48 }
49
50 10 ▶sub cgGetAttributes {
Subroutine cgGetAttributes: called CC=5 SCAR=16.1
Pod documented
51 10 my $self = shift;
52 10 my $classes = [ $self ];
53 10 $self->cgGetIsaTree($classes);
54 10 my @attributes = ();
55 {
56 # On perl 5.10.0, we have a warning message:
57 # "::AS" used only once: possible typo ...
58 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 10 for (@$classes) {
60 40 ▶ push @attributes, @{$_.'::AS'} if @{$_.'::AS'};
Branch: 1 branch
BranchTrueFalseTotal
if @{$_ . '::AS';} 10 30 40
61 40 ▶ push @attributes, @{$_.'::AA'} if @{$_.'::AA'};
Branch: 1 branch
BranchTrueFalseTotal
if @{$_ . '::AA';} 10 30 40
62 40 ▶ push @attributes, @{$_.'::AO'} if @{$_.'::AO'};
Branch: 1 branch
BranchTrueFalseTotal
if @{$_ . '::AO';} 10 30 40
63 }
64 }
65 10 \@attributes;
66 }
67  
68 4 ▶sub cgClone {
Subroutine cgClone: called CC=4 SCAR=15.2
Pod documented
69 4 my $self = shift;
70 4 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
71 4 ▶ return bless([ @$self ], $class)
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($self, "Class::Gomor::Array") 2 2 4
72 if UNIVERSAL::isa($self, 'Class::Gomor::Array');
73 2 ▶ return bless({ %$self }, $class)
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($self, "Class::Gomor::Hash") 2 0 2
74 if UNIVERSAL::isa($self, 'Class::Gomor::Hash');
75 0 $self;
76 }
77  
78 2 ▶sub cgFullClone {
Subroutine cgFullClone: called CC=1 SCAR=0.0
Pod documented
79 2 my $self = shift;
80 2 my ($n) = @_;
81 2 return [ map { $self->cgFullClone } 1..$n ];
82 }
83  
84 8 ▶sub cgBuildAccessorsScalar {
Subroutine cgBuildAccessorsScalar: called CC=2 SCAR=6.9
Pod documented
85 8 my $self = shift;
86 8 my ($accessors) = @_;
87 8 for my $a (@$accessors) {
88 8 ▶ *{$self.'::'.$a} = sub { shift->_cgAccessorScalar($a, @_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
89 8 }
90 }
91  
92 8 ▶sub cgBuildAccessorsArray {
Subroutine cgBuildAccessorsArray: called CC=2 SCAR=6.9
Pod documented
93 8 my $self = shift;
94 8 my ($accessors) = @_;
95 8 for my $a (@{$accessors}) {
96 8 ▶ *{$self.'::'.$a} = sub { shift->_cgAccessorArray($a, @_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
97 8 }
98 }
99  
100 0 ▶sub cgDebugPrint {
Subroutine cgDebugPrint: not called CC=3 SCAR=24.8
Pod documented
101 0 my $self = shift;
102 0 my ($level, $msg) = @_;
103 0 ▶ return if $Debug < $level;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::Gomor::Debug < $level 0 0 0
104 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
105 0 $class =~ s/^.*:://;
106 0 $msg =~ s/^/DEBUG: $class: /gm;
107 0 print STDERR $msg."\n";
108 }
109  
110 11 1;
111  
112 =head1 NAME
113  
114 Class::Gomor - another class and object builder
115  
116 =head1 DESCRIPTION
117  
118 This module is yet another class builder. This one adds parameter checking in B<new> constructor, that is to check for attributes existence, and definedness.
119  
120 In order to validate parameters, the module needs to find attributes, and that is the reason for declaring attributes in global variables named B<@AS>, B<@AA>, B<@AO>. They respectively state for Attributes Scalar, Attributes Array and Attributes Other. The last one is used to avoid autocreation of accessors, that is to let you declare your own ones.
121  
122 Attribute validation is performed by looking at classes hierarchy, by following @ISA tree inheritance.
123  
124 The loss in speed by validating all attributes is quite negligeable on a decent machine (Pentium IV, 2.4 GHz) with Perl 5.8.x. But if you want to avoid checking, you can do it, see below.
125  
126 This class is the base class for B<Class::Gomor::Array> and B<Class::Gomor::Hash>, so they will inherite the following methods.
127  
128 =head1 GLOBAL VARIABLES
129  
130 =over 4
131  
132 =item B<$NoCheck>
133  
134 Import it in your namespace like this:
135  
136 use Class::Gomor qw($NoCheck);
137  
138 If you want to disable B<cgCheckParams> to improve speed once your program is frozen, you can use this variable. Set it to 1 to disable parameter checking.
139  
140 =item B<$Debug>
141  
142 Import it in your namespace like this:
143  
144 use Class::Gomor qw($Debug);
145  
146 This variable is used by the B<cgDebugPrint> method.
147  
148 =back
149  
150 =head1 METHODS
151  
152 =over 4
153  
154 =item B<cgCheckParams> (hash ref, array ref)
155  
156 The attribute checking method takes two arguments, the first is user passed attributes (as a hash reference), the second is the list of valid attributes, gathered via B<cgGetAttributes> method (as an array ref). A message is displayed if passed parameters are not valid.
157  
158 =item B<cgGetIsaTree> (array ref)
159  
160 A recursive method. You pass a class in an array reference as an argument, and then the @ISA array is browsed, recursively. The array reference passed as an argument is increased with new classes, pushed into it. It returns nothing, result is stored in the array ref.
161  
162 =item B<cgGetAttributes>
163  
164 This method returns available attributes for caller's object class. It uses B<cgGetIsaTree> to search recursively in class hierarchy. It then returns an array reference with all possible attributes.
165  
166 =item B<cgBuildAccessorsScalar> (array ref)
167  
168 Accessor creation method. Takes an array reference containing all scalar attributes to create. Scalar accessors are stored in a global variable names B<@AS>. So you call this method at the beginning of your class like that:
169  
170 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
171  
172 =item B<cgBuildAccessorsArray> (array ref)
173  
174 Accessor creation method. Takes an array reference containing all array attributes to create. Array accessors are stored in a global variable names B<@AA>. So you call this method at the beginning of your class like that:
175  
176 __PACKAGE__->cgBuildAccessorsArray(\@AA);
177  
178 =item B<cgClone> [ (scalar) ]
179  
180 You can clone one of your objects by calling this method. An optional parameter may be used to create multiple clones. Cloning will occure only on the first level attributes, that is, if you have attributes containing other objects, they will not be cloned.
181  
182 =item B<cgFullClone> [ (scalar) ]
183  
184 This method is the same as B<cgClone>, but will clone all attributes recursively, but only if they are subclassed from B<Class::Gomor>. So, objects created with other modules than B<Class::Gomor::Array> or B<Class::Gomor::Hash> will not be cloned.
185  
186 Another thing to note, there is no catch for cycling references (when you link two objects with each others). You have been warned.
187  
188 =item B<cgDebugPrint> (scalar, scalar)
189  
190 First argument is a debug level. It is compared with global B<$Debug>, and if it is less than it, the second argument (a message string) is displayed. This method exists because I use it, maybe you will not like it.
191  
192 =back
193  
194 =head1 SEE ALSO
195  
196 L<Class::Gomor::Array>, L<Class::Gomor::Hash>
197  
198 =head1 AUTHOR
199
200 Patrice E<lt>GomoRE<gt> Auffret
201
202 =head1 COPYRIGHT AND LICENSE
203  
204 Copyright (c) 2004-2015, Patrice E<lt>GomoRE<gt> Auffret
205  
206 You may distribute this module under the terms of the Artistic license.
207 See LICENSE.Artistic file in the source distribution archive.
208  
209 =cut
↑ Summary Gomor/Array.pm »