HPLOO/Base.pm untested

filter: Statementstmt 0.0% 0 / 318 Branchbran 0.0% 0 / 204 Conditioncond 0.0% 0 / 86 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 700 CC 103 SCAR 57.8
CC
103
Cov
0%
CLASS_HPLOO_ATTR_TYPE
992.0
new
240.0
SUPER
210.0
CRAP 322.4
1 #############################################################################
2 ## Name: Base.pm
3 ## Purpose: Base class for HPLOO classes.
4 ## Author: Graciliano M. P.
5 ## Modified by:
6 ## Created: 30/10/2004
7 ## RCS-ID:
8 ## Copyright: (c) 2004 Graciliano M. P.
9 ## Licence: This program is free software; you can redistribute it and/or
10 ## modify it under the same terms as Perl itself
11 #############################################################################
12  
13 package Class::HPLOO::Base ;
14  
15 use 5.006 ;
16 use strict qw(vars) ;
17  
18 use vars qw($VERSION $SYNTAX @ISA) ;
19  
20 $VERSION = '0.17';
21  
22 ############
23 # EXPORTER #
24 ############
25  
26 require Exporter;
27 @ISA = qw(Exporter UNIVERSAL) ;
28  
29 our @EXPORT = qw(SUPER new GET_CLASS_HPLOO_HASH CLASS_HPLOO_TIE_KEYS ATTRS CLASS_HPLOO_ATTR CLASS_HPLOO_ATTR_TYPE) ;
30  
31 our @EXPORT_OK = @EXPORT ;
32
33 ########################
34 # GET_CLASS_HPLOO_HASH #
35 ########################
36  
37 sub GET_CLASS_HPLOO_HASH {
38 my $pack = ref($_[0]) || ($_[1] ? $_[1] : $_[0]) ;
39 return \%{$pack . '::CLASS_HPLOO'} ;
40 }
41  
42 #########
43 # SUPER #
44 #########
45  
46 sub SUPER {
47 my ($prev_pack , undef , undef , $sub0) = caller(1) ;
48 $prev_pack = undef if $prev_pack eq 'Class::HPLOO::Base' ;
49
50 my ($pack,$sub) = ( $sub0 =~ /^(?:(.*?)::|)(\w+)$/ );
51  
52 my $sub_is_new_hploo = $sub0 =~ /^(.*?(?:::)?$sub)\::$sub$/ ? 1 : undef ;
53
54 ##print "SUPER[$sub_is_new_hploo]>> @_ >> $pack,$sub >> $prev_pack\n" ;
55  
56 unshift(@_ , $prev_pack) if ( $sub_is_new_hploo && $prev_pack && ((!ref($_[0]) && $_[0] ne $prev_pack && !UNIVERSAL::isa($_[0] , $prev_pack)) || (ref($_[0]) && !UNIVERSAL::isa($_[0] , $prev_pack)) ) ) ;
57  
58 if ( defined @{"$pack\::ISA"} ) {
59 my $isa_sub = ISA_FIND_NEW($pack, ($sub_is_new_hploo?'new':$sub) ,1) ;
60  
61 my ($sub_name) = ( $isa_sub =~ /(\w+)$/gi );
62 if ( $sub0 ne $isa_sub && !ref($_[0]) && $isa_sub =~ /^(.*?(?:::)?$sub_name)\::$sub_name$/ ) {
63 @_ = ( bless({},$_[0]) , @_[1..$#_] ) ;
64 }
65
66 if ( $sub0 eq $isa_sub && UNIVERSAL::isa($_[0] , $pack) ) {
67 my @isa = Class::HPLOO::Base::FIND_SUPER_WALK( ref($_[0]) , $pack ) ;
68 my $pk = $isa[-1] ;
69 if ( $sub_is_new_hploo ) {
70 if ( UNIVERSAL::isa($pk , 'Class::HPLOO::Base') ) {
71 ($sub) = ( $pk =~ /(\w+)$/gi );
72 }
73 else { $sub = 'new' ;}
74 }
75
76 my $isa_sub = $pk->can($sub) ;
77 ##print "SUPER WALK>> $pk , $sub >> $isa_sub\n" ;
78 return &$isa_sub( ARGS_WRAPPER(@_) ) if $isa_sub ;
79 }
80
81 ##print "SUPER ISA>> $isa_sub >> $pack >> ". join(',',@{"$pack\::ISA"}) ."\n" ;
82 return &$isa_sub(@_) if $isa_sub && defined &$isa_sub && $sub0 ne $isa_sub ;
83 }
84
85 $sub = $sub_is_new_hploo ? 'new' : $sub ;
86 ##print "SUPER CALL>> $pack $sub >> @_\n" ;
87  
88 die("Can't find SUPER method for $sub0!") if "$pack\::$sub" eq $sub0 ;
89
90 return &{"$pack\::$sub"}(@_) ;
91 }
92  
93 ###################
94 # FIND_SUPER_WALK #
95 ###################
96  
97 sub FIND_SUPER_WALK {
98 my $class_main = shift ;
99 my $class_end = shift ;
100 my $only_stak = shift ;
101
102 my (@stack) ;
103 my $stack = $only_stak || {} ;
104
105 ##print "FIND>> $class_main , $class_end\n" ;
106
107 my $found ;
108 foreach my $isa_i ( @{"$class_main\::ISA"} ) {
109 next if $$stack{$isa_i}++ ;
110 $found = 1 if $isa_i eq $class_end ;
111 push(@stack , $isa_i , FIND_SUPER_WALK($isa_i , $class_end , $stack) );
112 }
113
114 return ($found ? @stack : ()) if $only_stak ;
115 return @stack ;
116 }
117  
118 ################
119 # ISA_FIND_NEW #
120 ################
121  
122 sub ISA_FIND_NEW {
123 my $pack = shift ;
124 my $sub = shift ;
125 my $look_deep = shift ;
126 my $count = shift ;
127 return if $count > 100 ;
128
129 ##print "ISA_FIND_NEW>> $pack >> $sub\n" ;
130
131 my ($sub_name) ;
132 if ( UNIVERSAL::isa($pack , 'Class::HPLOO::Base') ) {
133 ($sub_name) = $sub eq 'new' ? ( $pack =~ /(\w+)$/ ) : ($sub) ;
134 }
135 else { $sub_name = $sub ;}
136
137 my $isa_sub = "$pack\::$sub_name" ;
138
139 if ( $look_deep || !defined &$isa_sub ) {
140 foreach my $isa_i ( @{"$pack\::ISA"} ) {
141 next if $isa_i eq $pack || $isa_i eq 'Class::HPLOO::Base' ;
142 last if $isa_i eq 'UNIVERSAL' ;
143 $isa_sub = ISA_FIND_NEW($isa_i , $sub , 0 , $count+1) ;
144 last if $isa_sub ;
145 }
146 }
147
148 $isa_sub = undef if !defined &$isa_sub ;
149
150 ##print "%%> $pack >> $isa_sub\n" ;
151 return $isa_sub ;
152 }
153  
154 ##################
155 # NEW_CALL_BEGIN #
156 ##################
157  
158 sub new_call_BEGIN {
159 my $class = shift ; $class = ref($class) if ref($class) ;
160 my $this = $class ;
161
162 my @isas = \@{"$class\::ISA"} ;
163
164 foreach my $isas_i ( @isas ) {
165 foreach my $ISA_i ( @$isas_i ) {
166 if ( defined @{"$ISA_i\::ISA"} && @{"$ISA_i\::ISA"} > 2 ) {
167 push(@isas , \@{"$ISA_i\::ISA"}) ;
168 }
169 last if $ISA_i eq 'Class::HPLOO::Base' ;
170 my $ret ;
171 my ($sub) = ( $ISA_i =~ /(\w+)$/ );
172 $sub = "$ISA_i\::$sub\_BEGIN" ;
173 $ret = &$sub($this,@_) if defined &$sub ;
174 $this = $ret if $ret && UNIVERSAL::isa($ret,$class) ;
175 }
176 }
177
178 return $this ;
179 }
180  
181 ################
182 # NEW_CALL_END #
183 ################
184  
185 sub new_call_END {
186 my $class = shift ; $class = ref($class) if ref($class) ;
187
188 my @isas = \@{"$class\::ISA"} ;
189
190 foreach my $isas_i ( @isas ) {
191 foreach my $ISA_i ( @$isas_i ) {
192 if ( defined @{"$ISA_i\::ISA"} && @{"$ISA_i\::ISA"} > 2 ) {
193 push(@isas , \@{"$ISA_i\::ISA"}) ;
194 }
195 last if $ISA_i eq 'Class::HPLOO::Base' ;
196 my ($sub) = ( $ISA_i =~ /(\w+)$/ );
197 $sub = "$ISA_i\::$sub\_END" ;
198 &$sub(@_) if defined &$sub ;
199 }
200 }
201
202 return ;
203 }
204  
205 #######
206 # NEW #
207 #######
208  
209 my $NEW_REF = \&new ;
210  
211 sub new {
212 my $class = shift ;
213 my $this = ref($class) ? $class : undef ;
214
215 my $class_bless = $class ;
216 ($class,$class_bless,$this) = @$class if ref($class) eq 'ARRAY' ;
217  
218 $class = ref($class) if ref($class) ;
219 $class_bless = ref($class_bless) if ref($class_bless) ;
220
221 my ($class_end) = ( $class =~ /(\w+)$/ );
222
223 ##print "BASE NEW>> $class,$class_bless,$this >> $class_end >> @_ >> [". join(" ", @{"$class\::ISA"}) ."]\n" ;
224
225 if ( !defined &{"$class\::$class_end"} && @{"$class\::ISA"} > 1 ) {
226 foreach my $ISA_i ( @{"$class\::ISA"} ) {
227 last if $ISA_i eq 'Class::HPLOO::Base' ;
228 my $sub = "$ISA_i\::new" ;
229 if ( defined &$sub ) {
230 my $new_ref = \&$sub ;
231 return &$sub([$ISA_i,$class,$this],@_) if $new_ref == $NEW_REF || (defined &{"$ISA_i\::__CLASS__"} && defined &{"$ISA_i\::SUPER"} && defined &{"$ISA_i\::new_call_END"} ) ;
232 return &$sub($class,@_) ;
233 }
234 }
235 }
236  
237 $this ||= $class ;
238 $this = new_call_BEGIN( $this , @_) ;
239 $this = bless({} , $class_bless) if !ref($this) || !UNIVERSAL::isa($this,$class_bless) ;
240
241 no warnings ;
242
243 my $undef = \'' ;
244 sub UNDEF {$undef} ;
245
246 my $CLASS_HPLOO = GET_CLASS_HPLOO_HASH($this) ;
247
248 if ( $$CLASS_HPLOO{ATTR} ) { CLASS_HPLOO_TIE_KEYS($this) }
249
250 my $ret_this = defined &{"$class\::$class_end"} ? $this->$class_end(@_) : undef ;
251
252 if ( ref($ret_this) && UNIVERSAL::isa($ret_this,$class_bless) ) {
253 $this = $ret_this ;
254 if ( $$CLASS_HPLOO{ATTR} && UNIVERSAL::isa($this,'HASH') ) { CLASS_HPLOO_TIE_KEYS($this) }
255 }
256 elsif ( $ret_this == $undef ) { $this = undef }
257
258 new_call_END($class,$this,@_) ;
259  
260 return $this ;
261 }
262  
263 ########################
264 # CLASS_HPLOO_TIE_KEYS #
265 ########################
266
267 sub CLASS_HPLOO_TIE_KEYS {
268 my $this = shift ;
269 my $CLASS_HPLOO = GET_CLASS_HPLOO_HASH($this) ;
270 if ( $$CLASS_HPLOO{ATTR} ) {
271 foreach my $Key ( keys %{$$CLASS_HPLOO{ATTR}} ) {
272 tie( $this->{$Key} => 'Class::HPLOO::Base::HPLOO_TIESCALAR' , $this , $Key , $$CLASS_HPLOO{ATTR}{$Key}{tp} , $$CLASS_HPLOO{ATTR}{$Key}{pr} , \$this->{CLASS_HPLOO_ATTR}{$Key} , \$this->{CLASS_HPLOO_CHANGED} , ref($this) ) if !exists $this->{$Key} ;
273 }
274 }
275 }
276  
277 #########
278 # ATTRS #
279 #########
280  
281 sub ATTRS { return @{[@{ GET_CLASS_HPLOO_HASH($_[0] , scalar caller)->{ATTR_ORDER} }]} } ;
282  
283 ####################
284 # CLASS_HPLOO_ATTR #
285 ####################
286  
287 sub CLASS_HPLOO_ATTR {
288 my $class = caller ;
289  
290 my @attrs = split(/\s*,\s*/ , $_[0]) ;
291
292 my $CLASS_HPLOO = GET_CLASS_HPLOO_HASH( undef , $class ) ;
293  
294 foreach my $attrs_i ( @attrs ) {
295 $attrs_i =~ s/^\s+//s ;
296 $attrs_i =~ s/\s+$//s ;
297 my ($name) = ( $attrs_i =~ /(\w+)$/gi ) ;
298 my ($type) = ( $attrs_i =~ /^((?:\w+\s+)*?&?\w+|(?:\w+\s+)*?\w+(?:(?:::|\.)\w+)*)\s+\w+$/gi ) ;
299
300 my $type0 = $type ;
301 $type0 =~ s/\s+/ /gs ;
302
303 $type = lc($type) ;
304 $type =~ s/(?:^|\s*)bool$/boolean/gs ;
305 $type =~ s/(?:^|\s*)int$/integer/gs ;
306 $type =~ s/(?:^|\s*)float$/floating/gs ;
307 $type =~ s/(?:^|\s*)str$/string/gs ;
308 $type =~ s/(?:^|\s*)sub$/sub_$name/gs ;
309 $type =~ s/\s//gs ;
310
311 $type = 'any' if $type !~ /^(?:(?:ref)|(?:ref)?(?:array|hash)(?:boolean|integer|floating|string|sub_\w+|any|&\w+)|(?:ref)?(?:array|hash)|(?:array|hash)?(?:boolean|integer|floating|string|sub_\w+|any|&\w+))$/ ;
312  
313 if ( $type eq 'any' && $type0 =~ /^((?:ref\s*)?(?:array|hash) )?(\w+(?:(?:::|\.)\w+)*)$/ ) {
314 my ($tp1 , $tp2) = ($1 , $2) ;
315 $tp1 =~ s/\s+//gs ;
316 $tp2 = 'UNIVERSAL' if $tp2 =~ /^(?:obj|object)$/i ;
317 $tp2 =~ s/\.+/::/gs ;
318 $type = "$tp1$tp2" ;
319 }
320
321 my $parse_ref = $type =~ /^(?:array|hash)/ ? 1 : 0 ;
322
323 push(@{ $$CLASS_HPLOO{ATTR_ORDER} } , $name) if !$$CLASS_HPLOO{ATTR}{$name} ;
324
325 $$CLASS_HPLOO{ATTR}{$name}{tp} = $type ;
326 $$CLASS_HPLOO{ATTR}{$name}{pr} = $parse_ref ;
327  
328 my $return ;
329  
330 if ( $type =~ /^sub_(\w+)$/ ) {
331 my $sub = $1 ;
332 $return = qq~
333 return (&$sub(\$this,\@_))[0] if defined &$sub ;
334 return undef ;
335 ~ ;
336 }
337 else {
338 $return = $parse_ref ? qq~
339 ref(\$this->{CLASS_HPLOO_ATTR}{$name}) eq 'ARRAY' ? \@{\$this->{CLASS_HPLOO_ATTR}{$name}} :
340 ref(\$this->{CLASS_HPLOO_ATTR}{$name}) eq 'HASH' ? \%{\$this->{CLASS_HPLOO_ATTR}{$name}} :
341 \$this->{CLASS_HPLOO_ATTR}{$name}
342 ~ :
343 "\$this->{CLASS_HPLOO_ATTR}{$name}" ;
344 }
345  
346 eval(qq~
347 package $class ;
348 sub set_$name {
349 my \$this = shift ;
350 if ( !defined \$this->{$name} ) {
351 tie( \$this->{$name} => 'Class::HPLOO::Base::HPLOO_TIESCALAR' , \$this , '$name' , '$type' , $parse_ref , \\\$this->{CLASS_HPLOO_ATTR}{$name} , \\\$this->{CLASS_HPLOO_CHANGED} , ref(\$this) ) ;
352 }
353
354 \$this->{CLASS_HPLOO_CHANGED}{$name} = 1 ;
355 \$this->{CLASS_HPLOO_ATTR}{$name} = CLASS_HPLOO_ATTR_TYPE( ref(\$this) , '$type',\@_) ;
356 }
357 ~) if !defined &{"$class\::set_$name"} ;
358
359 eval(qq~
360 package $class ;
361 sub get_$name {
362 my \$this = shift ;
363 $return ;
364 }
365 ~) if !defined &{"$class\::get_$name"} ;
366 }
367 }
368  
369 #########################
370 # CLASS_HPLOO_ATTR_TYPE #
371 #########################
372  
373 sub CLASS_HPLOO_ATTR_TYPE {
374 my $class = shift ;
375 my $type = shift ;
376
377 if ($type eq 'any') { return $_[0] }
378 elsif ($type eq 'string') {
379 return "$_[0]" ;
380 }
381 elsif ($type eq 'boolean') {
382 return if $_[0] =~ /^(?:false|null|undef)$/i ;
383 return 1 if $_[0] ;
384 return ;
385 }
386 elsif ($type eq 'integer') {
387 my $val = $_[0] ;
388 my ($sig) = ( $val =~ /^(-)/ );
389 $val =~ s/[^0-9]//gs ;
390 $val = "$sig$val" ;
391 return $val ;
392 }
393 elsif ($type eq 'floating') {
394 my $val = $_[0] ;
395 $val =~ s/[\s_]+//gs ;
396 if ( $val !~ /^\d+\.\d+$/ ) {
397 ($val) = ( $val =~ /(\d+)/ ) ;
398 $val .= '.0' ;
399 }
400 return $val ;
401 }
402 elsif ($type =~ /^sub_(\w+)$/) {
403 my $sub = $1 ;
404 return (&$sub(@_))[0] if defined &$sub ;
405 }
406 elsif ($type =~ /^&(\w+)$/) {
407 my $sub = $1 ;
408 return (&$sub(@_))[0] if defined &$sub ;
409 }
410 elsif ($type eq 'ref') {
411 my $val = $_[0] ;
412 return $val if ref($val) ;
413 }
414 elsif ($type eq 'array') {
415 my @val = @_ ;
416 return \@val ;
417 }
418 elsif ($type eq 'hash') {
419 my %val = @_ ;
420 return \%val ;
421 }
422 elsif ($type eq 'refarray') {
423 my $val = $_[0] ;
424 return $val if ref($val) eq 'ARRAY' ;
425 }
426 elsif ($type eq 'refhash') {
427 my $val = $_[0] ;
428 return $val if ref($val) eq 'HASH' ;
429 }
430 elsif ($type =~ /^array(&?[\w:]+)/ ) {
431 my $tp = $1 ;
432 my @val = @_ ;
433 my $accept_undef = $tp =~ /^(?:any|string|boolean|integer|floating|sub_\w+|&\w+)$/ ? 1 : undef ;
434 if ( $accept_undef ) {
435 return [map { CLASS_HPLOO_ATTR_TYPE($class , $tp , $_) } @val] ;
436 }
437 else {
438 return [map { CLASS_HPLOO_ATTR_TYPE($class , $tp , $_) || () } @val] ;
439 }
440 }
441 elsif ($type =~ /^hash(&?[\w:]+)/ ) {
442 my $tp = $1 ;
443 my %val = @_ ;
444 foreach my $Key ( keys %val ) {
445 $val{$Key} = CLASS_HPLOO_ATTR_TYPE($class , $tp , $val{$Key}) ;
446 }
447 return \%val ;
448 }
449 elsif ($type =~ /^refarray(&?[\w:]+)/ ) {
450 my $tp = $1 ;
451 return undef if ref($_[0]) ne 'ARRAY' ;
452 my $ref = CLASS_HPLOO_ATTR_TYPE($class , "array$tp" , @{$_[0]}) ;
453 @{$_[0]} = @{$ref} ;
454 return $_[0] ;
455 }
456 elsif ($type =~ /^refhash(&?[\w:]+)/ ) {
457 my $tp = $1 ;
458 return undef if ref($_[0]) ne 'HASH' ;
459 my $ref = CLASS_HPLOO_ATTR_TYPE($class , "hash$tp" , %{$_[0]}) ;
460 %{$_[0]} = %{$ref} ;
461 return $_[0] ;
462 }
463 elsif ($type =~ /^\w+(?:::\w+)*$/ ) {
464 return( UNIVERSAL::isa($_[0] , $type) ? $_[0] : undef ) ;
465 }
466 return undef ;
467 }
468  
469 #######################################
470 # CLASS::HPLOO::BASE::HPLOO_TIESCALAR #
471 #######################################
472  
473 package Class::HPLOO::Base::HPLOO_TIESCALAR ;
474  
475 sub TIESCALAR {
476 shift ;
477 my $obj = shift ;
478 my $this = bless( { nm => $_[0] , tp => $_[1] , pr => $_[2] , rf => $_[3] , rfcg => $_[4] , pk => ($_[5] || scalar caller) } , __PACKAGE__ ) ;
479
480 if ( $this->{tp} =~ /^sub_(\w+)$/ ) {
481 my $CLASS_HPLOO = Class::HPLOO::Base::GET_CLASS_HPLOO_HASH( undef , $this->{pk} ) ;
482
483 if ( !ref($$CLASS_HPLOO{OBJ_TBL}) ) {
484 eval { require Hash::NoRef } ;
485 if ( !$@ ) {
486 $$CLASS_HPLOO{OBJ_TBL} = {} ;
487 tie( %{$$CLASS_HPLOO{OBJ_TBL}} , 'Hash::NoRef') ;
488 }
489 else { $@ = undef }
490 }
491  
492 $$CLASS_HPLOO{OBJ_TBL}{ ++$$CLASS_HPLOO{OBJ_TBL}{x} } = $obj ;
493 $this->{oid} = $$CLASS_HPLOO{OBJ_TBL}{x} ;
494 }
495  
496 return $this ;
497 }
498  
499 sub STORE {
500 my $this = shift ;
501 my $ref = $this->{rf} ;
502 my $ref_changed = $this->{rfcg} ;
503  
504 if ( $ref_changed ) {
505 if ( ref $$ref_changed ne 'HASH' ) { $$ref_changed = {} }
506 $$ref_changed->{$this->{nm}} = 1 ;
507 }
508  
509 if ( $this->{pr} ) {
510 my $tp = $this->{tp} =~ /^ref/ ? $this->{tp} : 'ref' . $this->{tp} ;
511 $$ref = &{"$this->{pk}::CLASS_HPLOO_ATTR_TYPE"}($this->{pk} , $tp , @_) ;
512 }
513 else {
514 $$ref = &{"$this->{pk}::CLASS_HPLOO_ATTR_TYPE"}($this->{pk} , $this->{tp} , @_) ;
515 }
516 }
517  
518 sub FETCH {
519 my $this = shift ;
520 my $ref = $this->{rf} ;
521
522 if ( $this->{tp} =~ /^sub_(\w+)$/ ) {
523 my $CLASS_HPLOO = Class::HPLOO::Base::GET_CLASS_HPLOO_HASH( undef , $this->{pk} ) ;
524 my $sub = $this->{pk} . '::' . $1 ;
525 my $obj = $$CLASS_HPLOO{OBJ_TBL}{ $this->{oid} } ;
526 return (&$sub($obj,@_))[0] if defined &$sub ;
527 }
528 else {
529 if ( $this->{tp} =~ /^(?:ref)?(?:array|hash)/ ) {
530 my $ref_changed = $this->{rfcg} ;
531 if ( $ref_changed ) {
532 if ( ref $$ref_changed ne 'HASH' ) { $$ref_changed = {} }
533 $$ref_changed->{$this->{nm}} = 1 ;
534 }
535 }
536 return $$ref ;
537 }
538 }
539  
540 sub UNTIE {}
541 sub DESTROY {}
542  
543  
544 #######
545 # END #
546 #######
547  
548 1;
549  
550