File Coverage

lib/Class/STL/Element.pm
Criterion Covered Total %
statement 76 89 85.3
branch 28 50 56.0
condition 34 90 37.7
subroutine 24 28 85.7
pod 0 19 0.0
total 162 276 58.7


line stmt bran cond sub pod time code
1             # vim:ts=4 sw=4
2             # ----------------------------------------------------------------------------------------------------
3             # Name : Class::STL::Element.pm
4             # Created : 22 February 2006
5             # Author : Mario Gaffiero (gaffie)
6             #
7             # Copyright 2006-2007 Mario Gaffiero.
8             #
9             # This file is part of Class::STL::Containers(TM).
10             #
11             # Class::STL::Containers is free software; you can redistribute it and/or modify
12             # it under the terms of the GNU General Public License as published by
13             # the Free Software Foundation; version 2 of the License.
14             #
15             # Class::STL::Containers is distributed in the hope that it will be useful,
16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             # GNU General Public License for more details.
19             #
20             # You should have received a copy of the GNU General Public License
21             # along with Class::STL::Containers; if not, write to the Free Software
22             # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23             # ----------------------------------------------------------------------------------------------------
24             # Modification History
25             # When Version Who What
26             # 14/03/2006 0.02 mg Fixed Class::STL::Element->new() function.
27             # ----------------------------------------------------------------------------------------------------
28             # TO DO:
29             # ----------------------------------------------------------------------------------------------------
30             require 5.005_62;
31 7     7   75 use strict;
  7         13  
  7         858  
32 7     7   32 use warnings;
  7         11  
  7         175  
33 7     7   31 use vars qw($VERSION $BUILD);
  7         12  
  7         246  
34 7     7   2573 use Class::STL::ClassMembers::DataMember;
  7         15  
  7         218  
35 7     7   2529 use Class::STL::ClassMembers::FunctionMember;
  7         20  
  7         61  
36             $VERSION = '0.20';
37             # ----------------------------------------------------------------------------------------------------
38             {
39             package Class::STL::Element;
40 7     7   255 use UNIVERSAL;
  7         12  
  7         16  
41 7     7   149 use Carp qw(confess);
  7         12  
  7         360  
42 7         24 use Class::STL::ClassMembers qw( data ),
43             Class::STL::ClassMembers::DataMember->new(name => 'data_type', default => 'string',
44 7     7   38 validate => '^(string|array|numeric|ref)$');
  7         9  
45 7     7   3076 use Class::STL::ClassMembers::Constructor;
  7         15  
  7         26  
46             sub new_extra # static function
47             {
48 1321     1321 0 2109 my $self = shift;
49 1321         2739 while (@_)
50             {
51 820         1323 my $p = shift;
52 820 100 66     1925 if (!ref($p) && exists(${$self->members()}{$p}))
  820 100 66     16256  
53             {
54 664         2589 shift;
55             }
56             elsif (!ref($p) && scalar @_ != 0) {
57 111         384 shift;
58             }
59             else {
60 45         920 $self->data($p); # new() called.
61             }
62             }
63 1321         27087 return $self;
64             }
65             sub eq # (element)
66             {
67 966     966 0 1599 my $self = shift;
68 966         1329 my $other = shift;
69 966 100 33     19014 return defined($self->data()) && ref($self->data()) && $self->data()->can('eq')
    50 66        
      66        
70             ? $self->data()->eq($other)
71             : $self->data_type() eq 'string'
72             ? defined($self->data()) && defined($other->data()) && $self->data() eq $other->data()
73             : defined($self->data()) && defined($other->data()) && $self->data() == $other->data();
74             }
75             sub ne # (element)
76             {
77 4     4 0 7 my $self = shift;
78 4         10 return !$self->eq(shift);
79             }
80             sub gt # (element)
81             {
82 22     22 0 42 my $self = shift;
83 22         40 my $other = shift;
84 22 100 33     453 return defined($self->data()) && ref($self->data()) && $self->data()->can('gt')
    50 66        
      66        
85             ? $self->data()->gt($other)
86             : $self->data_type() eq 'string'
87             ? defined($self->data()) && defined($other->data()) && $self->data() gt $other->data()
88             : defined($self->data()) && defined($other->data()) && $self->data() > $other->data();
89             }
90             sub lt # (element)
91             {
92 198     198 0 338 my $self = shift;
93 198         263 my $other = shift;
94 198 100 33     4167 return defined($self->data()) && ref($self->data()) && $self->data()->can('lt')
    50 66        
      66        
95             ? $self->data()->lt($other)
96             : $self->data_type() eq 'string'
97             ? defined($self->data()) && defined($other->data()) && $self->data() lt $other->data()
98             : defined($self->data()) && defined($other->data()) && $self->data() < $other->data();
99             }
100             sub ge # (element)
101             {
102 2     2 0 6 my $self = shift;
103 2         3 my $other = shift;
104 2 50 33     34 return defined($self->data()) && ref($self->data()) && $self->data()->can('ge')
    50 0        
      66        
105             ? $self->data()->ge($other)
106             : $self->data_type() eq 'string'
107             ? defined($self->data()) && defined($other->data()) && $self->data() ge $other->data()
108             : defined($self->data()) && defined($other->data()) && $self->data() >= $other->data();
109             }
110             sub le # (element)
111             {
112 2     2 0 5 my $self = shift;
113 2         3 my $other = shift;
114 2 50 33     38 return defined($self->data()) && ref($self->data()) && $self->data()->can('le')
    50 0        
      33        
115             ? $self->data()->le($other)
116             : $self->data_type() eq 'string'
117             ? defined($self->data()) && defined($other->data()) && $self->data() le $other->data()
118             : defined($self->data()) && defined($other->data()) && $self->data() <= $other->data();
119             }
120             sub cmp # (element)
121             {
122 230     230 0 362 my $self = shift;
123 230         292 my $other = shift;
124 230 100       447 return $self->eq($other) ? 0 : $self->lt($other) ? -1 : 1;
    100          
125             }
126             sub mod # (element)
127             {
128 0     0 0 0 my $self = shift;
129 0         0 my $other = shift;
130 0 0 0     0 return ref($self->data()) && $self->data()->can('mod')
131             ? $self->data()->mod($other)
132             : $self->data($self->data() % $other->data());
133             }
134             sub add # (element)
135             {
136 18     18 0 32 my $self = shift;
137 18         24 my $other = shift;
138 18 50 33     344 return ref($self->data()) && $self->data()->can('add')
139             ? $self->data()->add($other)
140             : $self->data($self->data() + $other->data());
141             }
142             sub subtract # (element)
143             {
144 0     0 0 0 my $self = shift;
145 0         0 my $other = shift;
146 0 0 0     0 return ref($self->data()) && $self->data()->can('subtract')
147             ? $self->data()->subtract($other)
148             : $self->data($self->data() - $other->data());
149             }
150             sub mult # (element)
151             {
152 10     10 0 18 my $self = shift;
153 10         26 my $other = shift;
154 10 50 33     218 return ref($self->data()) && $self->data()->can('mult')
155             ? $self->data()->mult($other)
156             : $self->data($self->data() * $other->data());
157             }
158             sub div # (element)
159             {
160 0     0 0 0 my $self = shift;
161 0         0 my $other = shift;
162 0 0 0     0 return ref($self->data()) && $self->data()->can('div')
163             ? $self->data()->div($other)
164             : $self->data($self->data() / $other->data());
165             }
166             sub and # (element)
167             {
168 5     5 0 14 my $self = shift;
169 5         8 my $other = shift;
170 5 50 33     99 return ref($self->data()) && $self->data()->can('and')
      66        
171             ? $self->data()->and($other)
172             : $self->data() && $other->data();
173             }
174             sub or # (element)
175             {
176 5     5 0 7 my $self = shift;
177 5         6 my $other = shift;
178 5 50 33     69 return ref($self->data()) && $self->data()->can('or')
      33        
179             ? $self->data()->or($other)
180             : $self->data() || $other->data();
181             }
182             sub match # (element)
183             {
184 0     0 0 0 my $self = shift;
185 0         0 my $other = shift;
186 0 0 0     0 return ref($self->data()) && $self->data()->can('match')
187             ? $self->data()->match($other)
188 0         0 : $self->data() =~ /@{[ $other->data() ]}/;
189             }
190             sub match_ic # (element)
191             {
192 5     5 0 12 my $self = shift;
193 5         9 my $other = shift;
194 5 50 33     139 return ref($self->data()) && $self->data()->can('match_ic')
195             ? $self->data()->match_ic($other)
196 5         124 : $self->data() =~ /@{[ $other->data() ]}/i;
197             }
198             sub neg # (void)
199             {
200 10     10 0 13 my $self = shift;
201 10 50 33     170 return ref($self->data()) && $self->data()->can('neg')
202             ? $self->data()->neg()
203             : $self->data(-($self->data()));
204             }
205             sub print # (void)
206             {
207 88     88 0 130 my $self = shift;
208 88 50 33     1619 return ref($self->data()) && $self->data()->can('print')
209             ? $self->data()->print()
210             : $self->data();
211             }
212             }
213             # ----------------------------------------------------------------------------------------------------
214             1;