IntrospectionMethods.pm

filter: Statementstmt 95.1% 514 / 540 Branchbran 78.7% 219 / 278 Conditioncond 66.6% 38 / 57 MC/DCmcdc 47.3% 18 / 38 Subroutinesub 93.1% 81 / 87 Podpod 36.0% 9 / 25 totaltotal 85.7% 879 / 1025 CC 170 SCAR 16.8
CC
170
Cov
88%
__ANON__
20.0
get_extended_info
12.0
make_methods
11.1
CRAP 5.3
↑ Summary IntrospectionMethods/Catalog.pm »
1 # (X)Emacs mode: -*- cperl -*-
2  
3 # $Author: domi $
4 # $Date: 2004/12/13 12:19:43 $
5 # $Name: $
6 # $Revision: 1.5 $
7  
8 package Class::IntrospectionMethods;
9  
10 =head1 NAME
11  
12 Class::IntrospectionMethods - creates methods with introspection
13  
14 =head1 SYNOPSIS
15  
16 use Class::IntrospectionMethods qw/make_methods/;
17  
18 make_methods
19 (
20 parent,
21 global_catalog =>
22 {
23 name => 'metacat',
24 list =>
25 [
26 [qw/foo/] => f_cat,
27 [qw/bar baz/] => b_cat,
28 ],
29 }
30 new_with_init => 'new',
31 get_set => [ qw /foo bar baz / ];
32 ) ;
33  
34 =head1 DESCRIPTION
35  
36 This module provides:
37  
38 =over
39  
40 =item *
41  
42 A way to set up a lot of get/set method. These get/set methods can
43 access plain scalars, array, hash. These scalar, hash or array can be
44 tied (See L<perltie>) with classes specified by the user. The element
45 of these arrays or hashes can be constrained to be object, tied
46 scalar.
47  
48 =item *
49  
50 A way to later query the object or class to retrieve the list of
51 methods (aka slots) created by this module.
52  
53 =item *
54  
55 A way to organize these slots in several catalogs.
56  
57 =item *
58  
59 When a slot contains object or tied scalars hashes or arrays, the
60 contained object can be queried for the container object.
61 In other words, the parent object (the one constructed by
62 C<Class::IntrospectionMethods> contains a child object in one of its
63 slots either as a plain object or an object hidden behind a tied
64 construct. C<Class::IntrospectionMethods> will provide the child
65 object a method to retrieve the parent object reference.
66  
67 =back
68  
69 For instance, you can use this module to create a tree where each node
70 or leaf is an object. In this case, this module provides methods to
71 navigate up the tree of objects with the installed "parent" method.
72  
73 In other words, this module provides special methods to enable the
74 user to navigate up or down a tree (or directed graph) using
75 introspection (to go down) and the "parent" method to go up.
76  
77 You may notice similarities between this module and
78 L<Class::MethodMaker>. In fact this module was written from
79 Class::MethodMaker v1.08, but it does not provide most of the fancy
80 methods of Class::MethodMaker. Only scalar, array and hash
81 accessors (with their tied and objects variants) are provided.
82  
83 Originally, the introspection and "parent" functionalities were
84 implemented in Class::MethodMaker. Unfortunately, they were not
85 accepted by Class::MethodMaker's author since they did not fit his
86 own vision of his module (fair enough).
87  
88 The old API of L<Class::MethodMaker> is provided as deprecated
89 methods. Using the new (and hopefully more consistent) API is
90 prefered.
91  
92 =cut
93  
94 # --------------------------------------------------------------
95  
96 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 15 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 # Inheritance -------------------------
100  
101 #use AutoLoader;
102 #use vars qw( @ISA );
103 #@ISA = qw ( AutoLoader );
104  
105 15 ▶use vars qw( $VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 15 require Exporter;
108 15 @ISA = qw(Exporter);
109 15 @EXPORT_OK = qw(make_methods set_obsolete_behavior set_parent_method_name);
110  
111 # Utility -----------------------------
112  
113 # Necessary for parent feature
114 15 ▶use Scalar::Util qw(isweak weaken) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 use Class::IntrospectionMethods::Catalog
116 15 ▶ qw/set_global_catalog set_method_info set_method_in_catalog/;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 use Class::IntrospectionMethods::Parent
118 15 ▶ qw/set_parent_method_name graft_parent_method/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 15 ▶use Carp qw( carp cluck croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 15 my $obsolete_behavior = 'carp' ;
123 15 my $support_legacy = 0 ;
124 15 my $legacy_object_init = 'cmm_init' ;
125  
126 15 $VERSION = sprintf "%d.%03d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
127  
128 =head1 Transition from Class::MethodMaker
129  
130 This module was forked from Class::MethodMaker v1.08. To ease
131 migration from older project (which include a proprietary project of
132 mine) using Class::MethodMaker to Class::IntrospectionMethods, a
133 compatiblity mode is provided. (although some features of
134 L<Class::MethodMaker> will not work. See
135 L<Class::IntrospectionMethods::Legacy> for details)
136  
137 You can use the following function to finely tune the compatibility
138 behavior to either croak, carp (See L<Carp> for details) or be silent.
139  
140 One note: I provide backward compatibility for Class::MethodMaker
141 v1.08 and the modification I made that were later refused. So you may
142 notice compatibility features that do not exist in Class::MethodMaker
143 v1.08.
144  
145 =head2 set_obsolete_behavior ( behavior, provide_legacy_method)
146  
147 C<behavior> is either C<skip>, C<carp> or C<croak>. (default is
148 C<carp>).
149  
150 C<provide_legacy_method> is either 1 or 0. Default 0. When set to one,
151 this module will provide methods that were only available in the
152 modified version of Class::MethodMaker v1.08.
153  
154 =cut
155  
156 sub set_obsolete_behavior
157 15 ▶ {
Subroutine set_obsolete_behavior: called CC=1 SCAR=0.0
Pod documented
158 15 ($obsolete_behavior, $support_legacy) = @_ ;
159 15 Class::IntrospectionMethods::Parent::set_obsolete_behavior (@_) ;
160 15 Class::IntrospectionMethods::Catalog::set_obsolete_behavior (@_) ;
161 }
162  
163 # internal
164 sub warn_obsolete
165 49 ▶ {
Subroutine warn_obsolete: called CC=2 SCAR=7.4
Pod undocumented
166 49 ▶ return if $obsolete_behavior eq 'skip' ;
Branch: 1 branch
BranchTrueFalseTotal
if $obsolete_behavior eq "skip" 49 0 49
167 15 ▶ no strict 'refs' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 0 $obsolete_behavior->(@_) ;
169 }
170  
171 499 ▶sub ima_method_maker { 1 };
Subroutine ima_method_maker: called CC=1 SCAR=0.0
Pod undocumented
172  
173 168 ▶sub find_target_class {
Subroutine find_target_class: called CC=3 SCAR=11.0
Pod undocumented
174 # Find the class to add the methods to. I'm assuming that it would
175 # be the first class in the caller() stack that's not a subsclass of
176 # IntrospectionMethods. If for some reason a sub-class of
177 # IntrospectionMethods also wanted to use IntrospectionMethods it
178 # could redefine ima_method_maker to return a false value and then
179 # $class would be set to it.
180 168 my $class;
181 168 my $i = 0;
182 168 while (1)
183 {
184 667 $class = (caller($i++))[0];
185 last unless ( $class->isa('Class::IntrospectionMethods')
186 and
187 667 ▶ &{$class->can ('ima_method_maker')} );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Class::IntrospectionMethods") and &{$class->can("ima_method_maker");} 168 499 667
Condition66%2/3
$class->isa("Class::IntrospectionMethods") and &{$class->can("ima_method_maker");}
!ll&&!rl&&r
1680499
MC/DC50%1/2
$class->isa("Class::IntrospectionMethods") and &{$class->can("ima_method_maker");}
$class->isa("Class::IntrospectionMethods") &{$class->can("ima_method_maker");}
Truth table66%2/3
A and B
A= $class->isa("Class::IntrospectionMethods")B= &{$class->can("ima_method_maker");}
ABresult
0X0
100
111
188 }
189 168 return $class;
190 }
191  
192 # -------------------------------------
193  
194 15 my %legacy_catalog ;
195  
196 15 my %default_user_options =
197 (
198 catalog_name => undef,
199  
200 # When set, any object stored in a slot (either plain, hashed or
201 # arrayed slot) will get a method to fetch the parent object.
202 provide_parent_method => 0 ,
203  
204 # method called after object creation to perform special
205 # initialisation. This specifies the default name
206 object_init_method => 'cim_init' ,
207  
208 #whether to autovivify object stored in slots
209 auto_vivify => 1
210 ) ;
211  
212 my $child_init = sub
213 48 ▶ {
Subroutine __ANON__: called CC=6 SCAR=19.9
214 48 my ($obj,$init_method) = @_ ;
215  
216 48 ▶ return unless defined $obj ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 48 48
217  
218 48 ▶ if (defined $init_method && $obj->can($init_method))
Branch: 2 branches
BranchTrueFalseTotal
if (defined $init_method and $obj->can($init_method)) { } 20 28 48
elsif ($support_legacy and $obj->can($legacy_object_init)) { } 0 29 29
Condition66%2/3
defined $init_method and $obj->can($init_method)
!ll&&!rl&&r
02820
MC/DC50%1/2
defined $init_method and $obj->can($init_method)
defined $init_method $obj->can($init_method)
Truth table66%2/3
A and B
A= defined $init_methodB= $obj->can($init_method)
ABresult
0X0
100
111
219 {
220 20 $obj->$init_method() ;
221 }
222 ▶ elsif ($support_legacy && $obj->can($legacy_object_init))
Condition66%2/3
$support_legacy and $obj->can($legacy_object_init)
!ll&&!rl&&r
1270
MC/DC0%0/2
$support_legacy and $obj->can($legacy_object_init)
$support_legacy $obj->can($legacy_object_init)
Truth table66%2/3
A and B
A= $support_legacyB= $obj->can($legacy_object_init)
ABresult
0X0
100
111
223 {
224 0 warn_obsolete("calling obsolete $legacy_object_init on ".ref($obj)) ;
225 0 $obj->$legacy_object_init() ;
226 }
227 15 } ;
228  
229 # set legacy catalog methods that were defined in modified version of
230 # Class::MethodMaker v1.08
231 sub set_legacy_methods
232 20 ▶ {
Subroutine set_legacy_methods: called CC=1 SCAR=0.0
Pod undocumented
233 20 my $target_class = shift ;
234  
235 return
236 (
237 CMM_CATALOG_LIST =>
238 2 ▶ sub {my $p = ref $_[0] ? shift : $target_class;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 1 2
239 2 $p->CMM_CATALOG_LEGACY()->all_catalog} ,
240  
241 CMM_CATALOG =>
242 12 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
243 12 ▶ my $p = ref($_[0]) ? shift : $target_class;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 9 3 12
244 12 ▶ my @catalog_names = scalar @_ ? @_ :
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 10 2 12
245 $p->CMM_CATALOG_LEGACY()->all_catalog ;
246 12 my @result = $p->CMM_CATALOG_LEGACY()->slot(@catalog_names);
247 12 ▶ return wantarray ? @result : \@result ;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 2 12
248 },
249  
250 CMM_SLOT_CATALOG => sub
251 5 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
252 5 ▶ my $p = ref $_[0] ? shift : $target_class;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 2 5
253 5 my $slot = shift ;
254 5 ▶ $p->CMM_CATALOG_LEGACY()->change($slot, shift) if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 4 5
255 5 my @r = $p->CMM_CATALOG_LEGACY()->catalog($slot);
256 5 return $r[0] ; # legacy method can only return 1 item
257 } ,
258  
259 CMM_SLOT_DETAIL =>
260 3 ▶ sub {my $p = ref $_[0] ? shift : $target_class;
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 3 3
261 3 my $res = $p->CMM_CATALOG_LEGACY()->info(shift);
262 3 ▶ return wantarray ? @$res : $res ;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
263 }
264 20 ) ;
265 }
266  
267 sub make_methods
268 20 ▶ {
Subroutine make_methods: called CC=11 SCAR=24.0
Pod undocumented
269 20 my (@args) = @_;
270  
271 20 my $target_class = find_target_class;
272  
273 20 my @legacy_catalog_methods = set_legacy_methods($target_class) ;
274  
275 # user option used through this call to make_methods. The copy is
276 # done to provide a closure.
277 20 my %user_options = %default_user_options ;
278  
279 # Each meta-method is responsible for calling install_methods() to
280 # get it's methods installed.
281 20 while (1)
282 {
283 88 ▶ my $meta_method = shift @args or last;
Branch: 1 branch
BranchTrueFalseTotal
my $meta_method = shift @args or last 68 20 88
284  
285 68 ▶ if ($meta_method =~ /^-?parent$/ )
Branch: 5 branches
BranchTrueFalseTotal
if ($meta_method =~ /^-?parent$/) { } 8 60 68
elsif ($meta_method =~ /^-?noparent$/) { } 0 60 60
elsif ($meta_method =~ /^-?catalog$/) { } 4 56 60
elsif ($meta_method =~ /^-?nocatalog$/) { } 1 55 56
elsif ($meta_method =~ /^-?global[_-]catalog$/i) { } 2 53 55
286 {
287 8 $user_options{provide_parent_method} = 1 ;
288 }
289 elsif ($meta_method =~ /^-?noparent$/ )
290 {
291 0 $user_options{provide_parent_method} = 0 ;
292 }
293 elsif ($meta_method =~ /^-?catalog$/)
294 {
295 # legacy mode
296 4 ▶ if ($support_legacy && not defined $legacy_catalog{$target_class})
Branch: 1 branch
BranchTrueFalseTotal
if ($support_legacy and not defined $legacy_catalog{$target_class}) 3 1 4
Condition66%2/3
$support_legacy and not defined $legacy_catalog{$target_class}
!ll&&!rl&&r
013
MC/DC50%1/2
$support_legacy and not defined $legacy_catalog{$target_class}
$support_legacy not defined $legacy_catalog{$target_class}
Truth table66%2/3
A and not(B)
A= $support_legacyB= not defined $legacy_catalog{$target_class}
ABresult
0X0
100
111
297 {
298 3 warn_obsolete("-catalog is deprecated");
299 3 my @legacy = ( name => 'CMM_CATALOG_LEGACY',
300 list => [] ) ;
301 3 my %meth = (set_global_catalog($target_class, @legacy),
302 @legacy_catalog_methods) ;
303 3 install_methods (%meth) ;
304 3 $legacy_catalog{$target_class} = 1;
305 }
306 4 $user_options{catalog_name} = shift @args ;
307 }
308 elsif ($meta_method =~ /^-?nocatalog$/)
309 {
310 1 $user_options{catalog_name} = undef ;
311 }
312 elsif ($meta_method =~ /^-?global[_-]catalog$/i)
313 {
314 2 my $struct = shift @args;
315 2 my (%meth) = set_global_catalog($target_class, %$struct) ;
316 2 install_methods (%meth) ;
317 2 $legacy_catalog{$target_class} = 1;
318 }
319 else
320 {
321 53 ▶ my $arg = shift @args or
Branch: 1 branch
BranchTrueFalseTotal
my $arg = shift @args or croak("make_methods: No arg for $meta_method") 53 0 53
322 croak "make_methods: No arg for $meta_method";
323 53 ▶ my @args = ref($arg) eq 'ARRAY' ? @$arg : ($arg);
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq 'ARRAY' ? : 38 15 53
324 15 ▶ no strict 'refs' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
325 #print "Calling $meta_method\n";
326 53 $meta_method->(\%user_options,@args);
327 }
328 }
329 }
330  
331 sub store_slot_in_catalog
332 90 ▶ {
Subroutine store_slot_in_catalog: called CC=2 SCAR=6.9
Pod undocumented
333 90 my $slot = shift ;
334 90 my $catalog_name = shift ;
335  
336 90 my $target_class = find_target_class;
337  
338 90 my @details = @_ ;
339 90 set_method_info($target_class, $slot, \@details) ;
340  
341 90 ▶ return unless defined $catalog_name ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $catalog_name 73 17 90
342  
343 17 set_method_in_catalog($target_class, $slot, $catalog_name) ;
344 }
345  
346 sub install_methods
347 58 ▶ {
Subroutine install_methods: called CC=4 SCAR=14.0
Pod undocumented
348 58 my (%methods) = @_;
349  
350 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
351  
352 58 my $target_class = find_target_class;
353 58 my $package = $target_class . "::";
354  
355 58 my ($name, $code);
356 58 ▶ while (($name, $code) = each %methods)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %methods) 670 58 728
357 {
358 # add the method unless it's already defined (which should only
359 # happen in the case of static methods, I think.)
360 670 my $reftype = ref $code;
361 670 ▶ if ( $reftype eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq 'CODE') { } 670 0 670
362 {
363 670 ▶ *{"$package$name"} = $code unless defined *{"$package$name"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{"$package$name";}{"CODE"} 670 0 670
364 }
365 else
366 {
367 0 croak "What do you expect me to do with this?: $code\n";
368 }
369 }
370 }
371  
372 =head1 CLASS INTROSPECTION
373  
374 Class::IntrospectionMethods provides a set of features that enable you
375 to query the available methods of a class. These methods can be
376 invoked as class methods or object methods. From now on, a class
377 created with Class::IntrospectionMethods will be called a CIMed class.
378  
379 The top-down introspection is triggered by the C<global_catalog>
380 option.
381  
382 =head2 slot query: the global_catalog option
383  
384 When set, the C<global_catalog> will invoke the
385 L<Class::IntrospectionMethods::Catalog/set_global_catalog>
386 function. This function will use the parameters you passed to the
387 C<global_catalog> option to install a new method in your class. E.g.,
388 this C<global_catalog> option:
389  
390 package Foo::Bar ;
391 use Class::IntrospectionMethods qw/make_methods/;
392  
393 make_methods
394 (
395 global_catalog =>
396 {
397 name => 'metacat',
398 list => [
399 [qw/foo bar baz/] => foo_cat,
400 [qw/a b z/] => alpha_cat,
401 ],
402 },
403 )
404  
405 will enable you to call:
406  
407 &Foo::Bar::metacat->all_catalog ; # return alpha_cat foo_cat
408 my $obj = Foo::Bar-> new;
409 $obj -> metacat->all_catalog ; # also return alpha_cat foo_cat
410  
411 See L<Class::IntrospectionMethods::Catalog> for:
412  
413 =over
414  
415 =item *
416  
417 The other informations you can retrieve through the global catalog.
418  
419 =item *
420  
421 How to move a slot from one catalog to another at run-time (only the
422 object catalog can be modified)
423  
424 =item *
425  
426 The distinction between the class catalog and the object catalog
427  
428 =back
429  
430 Note that IntrospectionMethods does not check whether the method
431 declared in global_catalog are actually created by
432 IntrospectionMethods or created elsewhere.
433  
434  
435 =head2 From slot to object: the parent option.
436  
437 If you use tied scalars (with the C<tie_scalar> or C<hash> method
438 types), or object method type, your tied scalars or objects may need
439 to call the parent CIMed object.
440  
441 For instance, if you want to implement error handling in your tied
442 scalar or objects that will call the parent CIMed object or display
443 error messages giving back to the user the slot name containing the
444 faulty object.
445  
446 So if you need to query the slot name, or index value (for C<hash> or
447 C<array> method types), or be able to call the parent object, you can
448 use the C<parent> option when creating the parent CIMed class:
449  
450 package FOO ;
451 use Class::IntrospectionMethods
452 'parent' ,
453 object => [ foo => 'My::Class' ];
454  
455 Using this option will graft I<one> attribute and its accessor
456 method. Be default, this attribute and accessor method will be named
457 C<cim_parent>, but this can be changed with C<set_parent_method_name>.
458  
459 This attribute contains (and the accessor method will return) a
460 C<Class::IntrospectionMethods::ParentInfo> object. This object
461 features methods C<index_value>, C<slot_name> and C<parent>.
462 See L<Class::IntrospectionMethods::Parent/"ParentInfo class"> for
463 more details.
464  
465 =over
466  
467 =item C<CMM_PARENT>
468  
469 Reference of the parent object.
470  
471 =item C<CMM_SLOT_NAME>
472  
473 slot name to use to get the child object from the parent.
474  
475 =item C<CMM_INDEX_VALUE>
476  
477 index value (for C<tie_tie_hash> method type) to use to get the child
478 object from the parent.
479  
480 =back
481  
482 When using the C<-parent> option, a C<CMM_PARENT>, C<CMM_SLOT_NAME>
483 and C<CMM_INDEX_VALUE> methods are also grafted to the child's
484 class.
485  
486 Here is an example to retrieve a parent object :
487  
488 package FOO ;
489 use ExtUtils::testlib;
490 '-parent' ,
491 object_tie_hash =>
492 [
493 {
494 slot => 'bar',
495 tie_hash => ['MyHash'],
496 class => ['MyObj', 'a' => 'foo']
497 }
498 ],
499 new => 'new';
500  
501 package main;
502  
503 my $o = new X;
504  
505 my $obj = $o->a('foo') ;
506 my $p= $obj->metadad->parent; # $p is $o
507  
508 See L<Class::IntrospectionMethods::Parent/EXAMPLE> for further
509 details
510  
511 =head1 SUPPORTED METHOD TYPES
512  
513 =head2 new
514  
515 Creates a basic constructor.
516  
517 Takes a single string or a reference to an array of strings as its
518 argument. For each string creates a simple method that creates and
519 returns an object of the appropriate class.
520  
521 This method may be called as a class method, as usual, or as in instance
522 method, in which case a new object of the same class as the instance
523 will be created.
524  
525 =cut
526  
527 sub new
528 9 ▶ {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
529 9 my ($user_options, @args) = @_;
530 9 my %methods;
531 9 foreach (@args)
532 {
533 $methods{$_} = sub
534 11 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.6
535 11 my $class = shift;
536 11 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
537 11 bless {}, $class;
538 9 };
539 }
540 9 install_methods(%methods);
541 }
542  
543 =head2 new_with_init
544  
545 Creates a basic constructor which calls a method named C<init> after
546 instantiating the object. The C<init> method should be defined in the
547 class using IntrospectionMethods.
548  
549 Takes a single string or a reference to an array of strings as its
550 argument. For each string creates a simple method that creates an
551 object of the appropriate class, calls C<init> on that object
552 propagating all arguments, before returning the object.
553  
554 This method may be called as a class method, as usual, or as in instance
555 method, in which case a new object of the same class as the instance
556 will be created.
557  
558 =cut
559  
560 1 ▶sub new_with_init {
Subroutine new_with_init: called CC=2 SCAR=7.0
Pod documented
561 1 my ($user_options, @args) = @_;
562 1 my %methods;
563 1 foreach (@args) {
564 1 my $field = $_;
565 1 ▶ $methods{$field} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
566 1 my $class = shift;
567 1 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
568 1 my $self = {};
569 1 bless $self, $class;
570 1 $self->init (@_);
571 1 return $self;
572 1 };
573 }
574 1 install_methods(%methods);
575 }
576  
577 # ----------------------------------------------------------------------------
578  
579 =head2 new_with_args
580  
581 Creates a basic constructor.
582  
583 Takes a single string or a reference to an array of strings as its
584 argument. For each string creates a simple method that creates and
585 returns an object of the appropriate class.
586  
587 This method may be called as a class method, as usual, or as in instance
588 method, in which case a new object of the same class as the instance
589 will be created.
590  
591 Constructor arguments will be stored as a key, value pairs in the
592 object. No check is done regarding the consistencies of the data
593 passed to the constructor and the accessor methods created.
594  
595 =cut
596  
597 sub new_with_args
598 1 ▶ {
Subroutine new_with_args: called CC=2 SCAR=7.0
Pod documented
599 1 my ($user_options, @args) = @_;
600 1 my %methods;
601 1 foreach (@args)
602 {
603 $methods{$_} = sub
604 1 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.2
605 1 my $class = shift;
606 1 my @c_args = @_ ;
607 1 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
608 1 my $self = { @c_args };
609 1 bless $self, $class;
610 1 };
611 }
612 1 install_methods(%methods);
613 }
614  
615 =head2 get_set
616  
617 Takes a single string or a reference to an array of strings as its
618 argument. Each string specifies a slot, for which accessor methods are
619 created. E.g.
620  
621 get_set => 'foo',
622 get_set => [qw/foo bar/],
623  
624 The accessor methods are, by default:
625  
626 =over 4
627  
628 =item x
629  
630 If an argument is provided, sets a new value for x. This is true even
631 if the argument is undef (cf. no argument, which does not set.)
632  
633 Returns (new) value.
634  
635 Value defaults to undef.
636  
637 =item clear_x
638  
639 Sets value to undef. This is exactly equivalent to
640  
641 $foo->x (undef)
642  
643 No return.
644  
645 =back
646  
647 This is your basic get/set method, and can be used for slots
648 containing any scalar value, including references to non-scalar
649 data. Note, however, that IntrospectionMethods has meta-methods that
650 define more useful sets of methods for slots containing references to
651 lists, hashes, and objects.
652  
653 =cut
654  
655 sub get_set
656 11 ▶ {
Subroutine get_set: called CC=2 SCAR=6.9
Pod documented
657 11 my ($user_options, @args) = @_;
658 11 my @methods;
659  
660 11 foreach my $arg (@args)
661 {
662 28 my $slot = $arg ;
663  
664 store_slot_in_catalog
665 28 ($arg, $user_options->{catalog_name}, slot_type => 'scalar') ;
666  
667 push @methods, $arg =>
668 sub
669 20 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
670 20 my $self = shift;
671 20 ▶ if ( @_ ) {$self->{$slot} = shift;}
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 6 14 20
672 14 else {$self->{$slot};}
673 28 };
674 }
675  
676 11 install_methods (@methods);
677 }
678  
679 =head2 object
680  
681 Creates methods for accessing a slot that contains an object of a given
682 class.
683  
684 object => [
685 phooey => { class => 'Foo' },
686 [ qw / bar1 bar2 bar3 / ] => { class => 'Bar'},
687 foo => { class => 'Baz'
688 constructor_args => [ set => 'it' ]},
689 [qw/dog fox/] => { class => 'Fob',
690 constructor_args => [ sound => 'bark' ] },
691 cat => { class => 'Fob',
692 constructor_args => [ sound => 'miaow' ]}
693  
694 tiger => { class => 'Special',
695 init => 'my_init' # method to call after creation
696 }
697 ]
698  
699 The main argument is an array reference. The array should contain a
700 set of C<< slot_name => hash_ref >> pairs. C<slot_name> can be an
701 array ref if you want to specify several slots the same way.
702  
703 The hash ref sub-arguments are parsed thus:
704  
705 =over 4
706  
707 =item class
708  
709 The class name of the stored object.
710  
711 =item constructor_args
712  
713 A array ref containing arguments that are passed to the C<new>
714 constructor.
715  
716 =item init_method
717  
718 Name of a initialisation method to call on the newly created object.
719 The method name defaults to C<cim_init>. In other words if the user
720 class feature a C<cim_init> method, this one will be called after
721 creation of the object.
722  
723 =back
724  
725 For each slot C<x>, the following methods are created:
726  
727 =over 4
728  
729 =item x
730  
731 A get/set method.
732  
733 If supplied with an object of an appropriate type, will set set the slot
734 to that value.
735  
736 Else, if the slot has no value, then an object is created by calling
737 C<new> on the appropriate class, passing in any supplied
738 arguments. These arguments may supersede the arguments passed with the
739 C<constructor_args> parameters (See above).
740  
741 The stored object is then returned.
742  
743 =item delete_x
744  
745 Will destroy the object held by C<x>.
746  
747 =item defined_x
748  
749 Will return true if C<x> contains an object. False otherwise.
750  
751 =back
752  
753 =cut
754  
755 sub translate_object_args
756 4 ▶ {
Subroutine translate_object_args: called CC=7 SCAR=19.5
Pod undocumented
757 4 my @old_args = @_ ;
758  
759 4 warn_obsolete( "Old style object arguments are deprecated. Check documentation");
760  
761 # translate old style api
762 4 my @new ;
763 4 ▶ while (@old_args)
Branch: 1 branch
BranchTrueFalseTotal
if (@old_args) 6 4 10
764 {
765 6 my $obj_class = shift @old_args;
766  
767 6 ▶ my $list = shift @old_args or die "No slot names for obj_class";
Branch: 1 branch
BranchTrueFalseTotal
my $list = shift @old_args or die "No slot names for obj_class" 6 0 6
768 # Allow a list of hashrefs.
769 6 ▶ my @list = ( ref($list) eq 'ARRAY' ) ? @$list : ($list);
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'ARRAY' ? : 3 3 6
770  
771 6 foreach my $obj_def (@list)
772 {
773 11 my (@name, @c_args);
774 11 ▶ if ( ref $obj_def eq 'HASH') # list of hash ref
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj_def eq 'HASH') { } 3 8 11
775 {
776 my $slot = delete $obj_def->{slot}
777 3 ▶ or die "No slot defined in object hash ref";
Branch: 1 branch
BranchTrueFalseTotal
my $slot = delete $obj_def->{"slot"} or die "No slot defined in object hash ref" 3 0 3
778 3 push @new , $slot, {%$obj_def, class => $obj_class} ;
779 }
780 else
781 {
782 8 push @new, $obj_def => $obj_class ;
783 }
784 }
785 }
786 4 return @new ;
787 }
788  
789 sub object
790 6 ▶ {
Subroutine object: called CC=10 SCAR=23.2
Pod documented
791 6 my ($user_options, @old_args) = @_;
792 6 my %methods;
793  
794 6 my $may_be_class = $old_args[0] ;
795  
796 # test whether the package name exists or not.
797 6 ▶ my @args = defined * {$may_be_class.'::'} ?
Branch: 1 branch
BranchTrueFalseTotal
defined *{$may_be_class . '::';} ? : 4 2 6
798 translate_object_args(@old_args) : @old_args ;
799  
800 # new style API: list of hash ref
801 6 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 13 6 19
802 {
803 13 my $slot_item = shift @args ;
804  
805 # Allow a list ref
806 13 ▶ my @slot_list = ( ref($slot_item) ) ? @$slot_item : ($slot_item);
Branch: 1 branch
BranchTrueFalseTotal
ref $slot_item ? : 1 12 13
807  
808 13 my $arg0 = shift @args ;
809 13 ▶ my $href = ref $arg0 ? $arg0 : {class => $arg0};
Branch: 1 branch
BranchTrueFalseTotal
ref $arg0 ? : 3 10 13
810 13 my $c_args = $href->{constructor_args} ;
811 13 my $slot_av = $href->{auto_vivify} ;
812 13 ▶ my $av = defined $slot_av ? $slot_av : $user_options->{auto_vivify} ;
Branch: 1 branch
BranchTrueFalseTotal
defined $slot_av ? : 0 13 13
813 13 my $graft = $user_options->{provide_parent_method} ;
814  
815 13 foreach my $slot (@slot_list)
816 {
817 # these lexicals will be used in closures
818 14 my $type = $href->{class} ;
819 14 ▶ my @c_args = defined $c_args ? @$c_args : () ;
Branch: 1 branch
BranchTrueFalseTotal
defined $c_args ? : 2 12 14
820 my $init_method = $href->{init_method}
821 14 ▶ || $user_options->{object_init_method};
Condition33%1/3
$href->{'init_method'} || $user_options->{'object_init_method'}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$href->{'init_method'} || $user_options->{'object_init_method'}
$href->{'init_method'} $user_options->{'object_init_method'}
Truth table33%1/3
A || B
A= $href->{'init_method'}B= $user_options->{'object_init_method'}
ABresult
1X1
011
000
822  
823 $methods{$slot} = sub
824 14 ▶ {
Subroutine __ANON__: called CC=8 SCAR=21.1
825 14 my ($self, @sub_args) = @_;
826  
827 14 ▶ if (not defined $self->{$slot} or scalar @sub_args > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{$slot} or scalar @sub_args > 0) 10 4 14
Condition66%2/3
not defined $self->{$slot} or scalar @sub_args > 0
l!l&&r!l&&!r
1004
MC/DC50%1/2
not defined $self->{$slot} or scalar @sub_args > 0
not defined $self->{$slot} scalar @sub_args > 0
Truth table66%2/3
not(A) or B
A= not defined $self->{$slot}B= scalar @sub_args > 0
ABresult
1X1
011
000
828 {
829 10 my $item = $sub_args[0];
830  
831 10 ▶ my $obj = (ref $item and UNIVERSAL::isa($item, $type)) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $item && UNIVERSAL::isa($item, $type) ? : 1 9 10
$av ? : 9 0 9
Condition66%2/3
ref $item and UNIVERSAL::isa($item, $type)
!ll&&!rl&&r
901
MC/DC50%1/2
ref $item and UNIVERSAL::isa($item, $type)
ref $item UNIVERSAL::isa($item, $type)
Truth table66%2/3
A and B
A= ref $itemB= UNIVERSAL::isa($item, $type)
ABresult
0X0
100
111
832 $item : $av ? $type->new(@c_args) : undef ;
833  
834 10 ▶ graft_parent_method($obj,$self, $slot)
Branch: 1 branch
BranchTrueFalseTotal
if $graft and defined $obj 6 4 10
Condition66%2/3
$graft and defined $obj
!ll&&!rl&&r
406
MC/DC50%1/2
$graft and defined $obj
$graft defined $obj
Truth table66%2/3
A and B
A= $graftB= defined $obj
ABresult
0X0
100
111
835 if $graft && defined $obj;
836  
837 10 $child_init->($obj, $init_method) ;
838  
839 # store object
840 10 $self->{$slot} = $obj;
841 }
842  
843 14 return $self->{$slot};
844 14 };
845  
846 store_slot_in_catalog
847 (
848 $slot, $user_options->{catalog_name},
849 14 ▶ slot_type => 'scalar',
Branch: 1 branch
BranchTrueFalseTotal
scalar @c_args ? : 2 12 14
850 class => $type,
851 scalar @c_args ? (class_args => \@c_args) : ()
852 ) ;
853  
854 0 ▶ $methods{"delete_$slot"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
855 0 my ($self) = @_;
856 0 $self->{$slot} = undef;
857 14 };
858  
859 2 ▶ $methods{"defined_$slot"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
860 2 my ($self) = @_;
861 2 ▶ return defined $self->{$slot} ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$slot} ? : 1 1 2
862 14 };
863 }
864 }
865 6 install_methods(%methods);
866 }
867  
868  
869 # ----------------------------------------------------------------------------
870  
871 =head2 tie_scalar
872  
873 Create a get/set method to deal with the tied scalar.
874  
875 Takes a list of pairs, where the first is the name of the slot (or an
876 array ref containing a list of slots), the second is an array
877 reference. The array reference takes the usual tie parameters.
878  
879 For instance if Enum and Boolean are tied scalar that accept default values,
880 you can have:
881  
882 tie_scalar =>
883 [
884 foo => [ 'Enum', enum => [qw/A B C/], default => 'B' ],
885 bar => [ 'Enum', enum => [qw/T0 T1/], default => 'T1'],
886 baz => ['Boolean', default => 0],
887 [qw/lots of slots/] => ['Boolean', default => 1],
888 ],
889  
890 Foreach slot C<xx>, tie_scalar install the following methods:
891  
892 =over
893  
894 =item tied_storage_xx
895  
896 Return the object tied behind the scalar. Auto-vivify if necessary.
897  
898 =back
899  
900 =cut
901  
902 sub tie_scalar
903 5 ▶ {
Subroutine tie_scalar: called CC=8 SCAR=20.8
Pod documented
904 5 my ($user_options, @args) = @_;
905 5 my %methods;
906  
907 5 my $parent_method_closure = $user_options->{provide_parent_method} ;
908  
909 5 ▶ while ( my ($fieldr, $tie_args) = splice (@args, 0, 2))
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 6 5 11
910 {
911 6 ▶ my ($tie_class,@c_args)= ref($tie_args) ? @$tie_args : ($tie_args);
Branch: 1 branch
BranchTrueFalseTotal
ref $tie_args ? : 5 1 6
912  
913 6 ▶ croak "undefined tie class" unless defined $tie_class ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tie_class 0 6 6
914  
915 6 ▶ foreach my $field_elt (ref $fieldr ? @$fieldr : $fieldr)
Branch: 1 branch
BranchTrueFalseTotal
ref $fieldr ? : 1 5 6
916 {
917 8 my $field = $field_elt ; # safer with the closures below
918  
919 my $create_field = sub
920 5 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
921 5 my $self = shift ;
922 # directly tie the scalar held by self
923 5 my $obj = tie ($self->{$field}, $tie_class, @c_args);
924  
925 5 ▶ graft_parent_method($obj,$self,$field)
Branch: 1 branch
BranchTrueFalseTotal
if $parent_method_closure 3 2 5
926 if $parent_method_closure ;
927 8 } ;
928  
929 $methods{$field} =
930 sub
931 9 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
932 9 my $self = shift ;
933  
934 9 ▶ &$create_field($self) unless exists $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$field} 4 5 9
935  
936 9 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 6 9
937 {
938 3 $self->{$field} = $_[0] ;
939 # avoid reading $$ref which can be a tied ref
940 3 return $_[0] ;
941 }
942  
943 6 return $self->{$field} ;
944 8 };
945  
946 my $tied_storage_sub = sub
947 3 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
948 3 my $self = shift ;
949 # create the tied variable if necessary
950 # (i.e. accessor was not used before)
951 3 ▶ &$create_field($self) unless exists $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$field} 1 2 3
952  
953 3 return tied($self->{$field}) ;
954 8 };
955  
956 # first method provides name consistency with tie_tie_hash
957 8 $methods{"tied_storage_$field"} = $tied_storage_sub ;
958  
959 8 foreach my $deprecated ("tied_scalar_$field",
960 "tied_$field",
961 $field."_tied")
962 {
963 $methods{$deprecated} = sub
964 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
965 3 warn_obsolete("method $deprecated is deprecated") ;
966 3 return $tied_storage_sub->(@_) ;
967 24 } ;
968 }
969  
970 store_slot_in_catalog
971 (
972 $field, $user_options->{catalog_name},
973 8 ▶ slot_type => 'scalar',
Branch: 1 branch
BranchTrueFalseTotal
scalar @c_args ? : 7 1 8
974 tie_scalar => $tie_class,
975 scalar @c_args ? (tie_scalar_args => \@c_args) : ()
976 );
977 }
978  
979 }
980 5 install_methods(%methods);
981 }
982  
983  
984 28 ▶sub _add_hash_methods {
Subroutine _add_hash_methods: called CC=2 SCAR=7.4
985 28 my ($methods, $field, $create_hash) = @_ ;
986  
987 28 ▶ croak "Missing create_hash sub" unless defined $create_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $create_hash 0 28 28
988  
989 $methods->{$field . "_keys"} =
990 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
991 3 my ($self) = @_;
992 3 ▶ &$create_hash($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 3 3
993 3 return keys %{$self->{$field}} ;
994 28 };
995  
996 $methods->{$field . "_values"} =
997 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
998 2 my ($self) = @_;
999 2 ▶ &$create_hash($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 2 2
1000 2 values %{$self->{$field}} ;
1001 28 };
1002  
1003 $methods->{$field . "_exists"} =
1004 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1005 0 my ($self) = shift;
1006 0 my ($key) = @_;
1007 return
1008 0 ▶ exists $self->{$field} && exists $self->{$field}{$key};
Condition0%0/3
exists $self->{$field} && exists $self->{$field}{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$field} && exists $self->{$field}{$key}
exists $self->{$field} exists $self->{$field}{$key}
Truth table0%0/3
A && B
A= exists $self->{$field}B= exists $self->{$field}{$key}
ABresult
0X0
100
111
1009 28 };
1010  
1011 $methods->{$field . "_delete"} =
1012 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1013 2 my ($self, @keys) = @_;
1014 2 ▶ &$create_hash($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 2 2
1015 2 delete @{$self->{$field}}{@keys};
1016 28 };
1017  
1018 $methods->{$field . "_clear"} =
1019 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1020 1 my $self = shift;
1021 1 ▶ &$create_hash($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 1 1
1022 1 %{$self->{$field}} = ();
1023 28 };
1024  
1025 $methods->{$field . "_index"} =
1026 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1027 0 my $self = shift;
1028 0 $self->$field(@_) ;
1029 28 };
1030  
1031 $methods->{$field . "_set"} =
1032 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1033 3 my $self = shift;
1034 3 ▶ &$create_hash($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 3 3
1035 3 %{$self->{$field}} = (@_);
1036 28 };
1037 }
1038  
1039 # ----------------------------------------------------------------------------
1040  
1041 =head2 hash
1042  
1043 Creates a group of methods for dealing with hash data stored in a
1044 slot.
1045  
1046 hash =>
1047 [
1048 'plain_hash1', 'plain_hash2',
1049 [qw/lot of plain hashes/] ,
1050 yet_another_plain_hash => {} ,
1051  
1052 my_tied_hash => {tied_hash => 'My_Tie_Hash' },
1053 my_tied_hash_with_args =>
1054 { tied_hash => [ 'My_Tie_Hash' , @my_args ] },
1055  
1056 my_hash_with_tied_storage => { tie_storage => 'MyTieScalar' },
1057 [qw/likewise_with_args likewise_with_other_args/] =>
1058 { tie_storage => [ 'MyTieScalar', @my_args] }
1059  
1060 my_tied_hash_with_tied_storage =>
1061 { tied_hash => 'My_Tie_Hash',tie_storage => 'MyTieScalar' },
1062  
1063 my_hash_with_object => { class_storage => 'MyClass' },
1064 my_hash_with_object_and_constructor_args =>
1065 { class_storage => [ 'MyClass' , @my_args ] },
1066  
1067 ]
1068  
1069  
1070 The C<hash> parameters are:
1071  
1072 =over
1073  
1074 =item *
1075  
1076 A string or a a reference to an array of strings. For each
1077 of these string, a hash based slot is created.
1078  
1079 =item *
1080  
1081 A hash ref who contains attributes attached to the slot(s) defined by
1082 the previous arguments. These attribute are used to specify the
1083 behavior of the hash attached to the slot or to specialize the hash
1084 values. See L<Tie::Hash::CustomStorage> for details on the possibles
1085 attributes.
1086  
1087 =back
1088  
1089 For each slot defined, creates:
1090  
1091 =over 4
1092  
1093 =item x
1094  
1095 Called with no arguments returns the hash stored in the slot, as a hash
1096 in a list context or as a reference in a scalar context.
1097  
1098 Called with one simple scalar argument it treats the argument as a key
1099 and returns the value stored under that key.
1100  
1101 Called with more than one argument, treats them as a series of key/value
1102 pairs and adds them to the hash.
1103  
1104 =item x_keys or x_index
1105  
1106 Returns the keys of the hash.
1107  
1108 =item x_values
1109  
1110 Returns the list of values.
1111  
1112 =item x_exists
1113  
1114 Takes a single key, returns whether that key exists in the hash.
1115  
1116 =item x_delete
1117  
1118 Takes a list, deletes each key from the hash.
1119  
1120 =item x_clear
1121  
1122 Resets hash to empty.
1123  
1124 =back
1125  
1126 =cut
1127  
1128 sub hash
1129 12 ▶ {
Subroutine hash: called CC=9 SCAR=22.0
Pod documented
1130 12 my ($user_options, @args) = @_;
1131 12 my %methods;
1132  
1133 #print "hash called with\n", Dumper $user_options, Dumper \@args ;
1134  
1135 12 require Tie::Hash::CustomStorage ;
1136  
1137 12 my $parent_method_closure = $user_options->{provide_parent_method} ;
1138  
1139 12 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 24 12 36
1140 {
1141 24 my $hash = shift @args ;
1142 24 ▶ my @slot_hash = ( ref($hash) eq 'ARRAY' ) ? @$hash : ($hash);
Branch: 1 branch
BranchTrueFalseTotal
ref $hash eq 'ARRAY' ? : 4 20 24
1143  
1144 24 ▶ my $x_parm = ref $args[0] ? shift @args : undef ;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 21 3 24
1145 24 my $init_meth = $user_options->{object_init_method} ;
1146 my $create_hash = sub
1147 17 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.1
1148 17 my ($self,$name) = @_ ;
1149 17 my %hash ;
1150 17 ▶ if (defined $x_parm)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm) 15 2 17
1151 {
1152 my $init_obj = sub
1153 14 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.1
1154 14 my ($l_obj,$l_idx) = @_ ;
1155 14 ▶ graft_parent_method($l_obj,$self,$name,$l_idx)
Branch: 1 branch
BranchTrueFalseTotal
if $parent_method_closure 14 0 14
1156 if $parent_method_closure ;
1157 14 $child_init->($l_obj, $init_meth) ;
1158 15 } ;
1159  
1160 15 my $custom_tied_obj = tie %hash, 'Tie::Hash::CustomStorage', %$x_parm,
1161 init_object => $init_obj ;
1162  
1163 15 ▶ my $user_tied_obj = $custom_tied_obj->get_user_tied_hash_object
Branch: 1 branch
BranchTrueFalseTotal
if defined $custom_tied_obj 15 0 15
1164 if defined $custom_tied_obj;
1165 15 ▶ graft_parent_method($user_tied_obj,$self,$name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $user_tied_obj and $parent_method_closure 9 6 15
Condition100%3/3
defined $user_tied_obj and $parent_method_closure
!ll&&!rl&&r
429
MC/DC100%2/2
defined $user_tied_obj and $parent_method_closure
defined $user_tied_obj $parent_method_closure
Truth table100%3/3
A and B
A= defined $user_tied_objB= $parent_method_closure
ABresult
0X0
100
111
1166 if defined $user_tied_obj and $parent_method_closure ;
1167 }
1168 17 $self->{$name} = \%hash ;
1169 24 };
1170  
1171 my $handle_value = sub
1172 6 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
1173 6 my ($self,$name,$key) = splice @_,0,3 ;
1174 6 ▶ return undef unless defined $key ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 6 6
1175  
1176 #print "assigning $_[0]\n";
1177 6 ▶ $self->{$name}{$key} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
1178 6 ▶ return @_ ? $_[0] : $self->{$name}{$key};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 6 6
1179 24 } ;
1180  
1181 24 foreach my $obj_def (@slot_hash)
1182 {
1183 28 my $name = $obj_def; # kept for closures
1184  
1185 $methods{$name} = sub
1186 67 ▶ {
Subroutine __ANON__: called CC=7 SCAR=19.5
1187 67 my ($self, $key) = splice @_,0,2;
1188  
1189 67 ▶ &$create_hash($self,$name) unless defined $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$name} 16 51 67
1190  
1191 67 ▶ return wantarray ? %{$self->{$name}} : $self->{$name}
Branch: 2 branches
BranchTrueFalseTotal
unless defined $key 17 50 67
wantarray ? : 1 16 17
1192 unless defined $key;
1193  
1194 50 ▶ croak "hash cannot have more than 2 arg"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 50 50
1195 if @_ >1 ;
1196  
1197 50 ▶ $self->{$name}{$key} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 15 35 50
1198 50 ▶ return @_ ? $_[0] : $self->{$name}{$key};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 15 35 50
1199 28 };
1200  
1201 my $tied_hash_sub = sub
1202 7 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
1203 7 my $self = shift ;
1204 7 ▶ $create_hash->($self,$name) unless defined $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$name} 1 6 7
1205 7 my $custom_tied_obj = tied(%{$self->{$name}}) ;
1206 7 ▶ return undef unless defined $custom_tied_obj ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $custom_tied_obj 0 7 7
1207 7 return $custom_tied_obj->get_user_tied_hash_object ;
1208 28 } ;
1209  
1210 28 ▶ if (defined $x_parm and defined $x_parm->{tie_hash})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm and defined $x_parm->{"tie_hash"}) 19 9 28
Condition100%3/3
defined $x_parm and defined $x_parm->{"tie_hash"}
!ll&&!rl&&r
3619
MC/DC100%2/2
defined $x_parm and defined $x_parm->{"tie_hash"}
defined $x_parm defined $x_parm->{"tie_hash"}
Truth table100%3/3
A and B
A= defined $x_parmB= defined $x_parm->{"tie_hash"}
ABresult
0X0
100
111
1211 {
1212 19 $methods{"tied_hash_$name"} = $tied_hash_sub ;
1213  
1214 $methods{"tied_$name"} =
1215 sub
1216 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
1217 3 warn_obsolete( "method tied_$name is deprecated") ;
1218 3 return $tied_hash_sub->(@_) ;
1219 19 } ;
1220 }
1221  
1222 my $tied_storage_sub = sub
1223 6 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
1224 6 my $self = shift ;
1225 6 my $idx = shift ;
1226 6 ▶ &$create_hash($self,$name) unless defined $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$name} 0 6 6
1227 6 &$handle_value($self,$name,$idx) ;
1228 6 my $ref = $self->{$name} ;
1229 6 return tied(%$ref)->get_tied_storage_object($idx) ;
1230 28 } ;
1231  
1232 28 ▶ if (defined $x_parm and defined $x_parm->{tie_storage})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm and defined $x_parm->{"tie_storage"}) 2 26 28
Condition100%3/3
defined $x_parm and defined $x_parm->{"tie_storage"}
!ll&&!rl&&r
3232
MC/DC100%2/2
defined $x_parm and defined $x_parm->{"tie_storage"}
defined $x_parm defined $x_parm->{"tie_storage"}
Truth table100%3/3
A and B
A= defined $x_parmB= defined $x_parm->{"tie_storage"}
ABresult
0X0
100
111
1233 {
1234 2 $methods{"tied_storage_$name"} = $tied_storage_sub ;
1235 $methods{"tied_scalar_$name"} = sub
1236 6 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
1237 6 warn_obsolete( "method tied_scalar_$name is deprecated") ;
1238 6 return $tied_storage_sub->(@_) ;
1239 2 } ;
1240 }
1241  
1242 28 my @info = get_extended_info($x_parm) ;
1243  
1244 store_slot_in_catalog($name, $user_options->{catalog_name},
1245 28 slot_type => 'hash', @info);
1246  
1247 28 _add_hash_methods(\%methods, $name,$create_hash);
1248 }
1249 }
1250 12 install_methods(%methods);
1251 }
1252  
1253 sub get_extended_info
1254 40 ▶ {
Subroutine get_extended_info: called CC=12 SCAR=24.9
Pod undocumented
1255 40 my $x_parm = shift ;
1256  
1257 #print Dumper $x_parm ;
1258  
1259 40 my @result = () ;
1260 40 ▶ return @result unless defined $x_parm ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x_parm 6 34 40
1261  
1262 34 ▶ if (defined $x_parm->{class_storage})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm->{"class_storage"}) 24 10 34
1263 {
1264 24 my $cs = $x_parm->{class_storage} ;
1265 24 ▶ my ($c,@args) = ref $cs ? @$cs : ($cs);
Branch: 1 branch
BranchTrueFalseTotal
ref $cs ? : 22 2 24
1266 24 push @result, class => $c ;
1267 24 ▶ push @result, class_args => \@args if @args ;
Branch: 1 branch
BranchTrueFalseTotal
if @args 22 2 24
1268 }
1269  
1270 34 ▶ if (defined $x_parm->{tie_storage})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm->{"tie_storage"}) 2 32 34
1271 {
1272 2 my $th = $x_parm->{tie_storage} ;
1273 2 ▶ my ($c,@args)= ref $th ? @$th : ($th);
Branch: 1 branch
BranchTrueFalseTotal
ref $th ? : 2 0 2
1274 2 push @result, tie_storage => $c;
1275 2 ▶ push (@result, tie_storage_args => \@args) if scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 0 2 2
1276 }
1277  
1278 34 ▶ my $tie_index = $x_parm->{tie_hash} || $x_parm->{tie_array} ;
Condition100%3/3
$x_parm->{'tie_hash'} || $x_parm->{'tie_array'}
l!l&&r!l&&!r
1978
MC/DC100%2/2
$x_parm->{'tie_hash'} || $x_parm->{'tie_array'}
$x_parm->{'tie_hash'} $x_parm->{'tie_array'}
Truth table100%3/3
A || B
A= $x_parm->{'tie_hash'}B= $x_parm->{'tie_array'}
ABresult
1X1
011
000
1279  
1280 34 ▶ if (defined $tie_index)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tie_index) 26 8 34
1281 {
1282 26 ▶ my ($c,@args)= ref $tie_index ? @$tie_index : ($tie_index);
Branch: 1 branch
BranchTrueFalseTotal
ref $tie_index ? : 25 1 26
1283 26 push @result, tie_index => $c;
1284 26 ▶ push (@result, tie_index_args => \@args) if scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 9 17 26
1285 }
1286  
1287 34 return @result ;
1288 }
1289  
1290  
1291 sub object_tie_hash
1292 6 ▶ {
Subroutine object_tie_hash: called CC=4 SCAR=14.0
Pod undocumented
1293 6 my ($user_options, @args) = @_;
1294  
1295 6 warn_obsolete( "object_tie_hash is deprecated. Please use hash instead");
1296  
1297 6 my @new ;
1298 6 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 9 6 15
1299 {
1300 9 my $hash = shift @args;
1301 my $slot = delete $hash->{slot}
1302 9 ▶ or croak "No slot names passef to object_tie_hash";
Branch: 1 branch
BranchTrueFalseTotal
my $slot = delete $hash->{"slot"} or croak("No slot names passef to object_tie_hash") 9 0 9
1303  
1304 $hash->{class_storage} = delete $hash->{class}
1305 9 ▶ or croak "No class passed to object_tie_hash";
Branch: 1 branch
BranchTrueFalseTotal
$hash->{"class_storage"} = delete $hash->{"class"} or croak("No class passed to object_tie_hash") 9 0 9
1306  
1307 9 push @new, $slot, $hash ;
1308 }
1309  
1310 6 hash($user_options, @new ) ;
1311 }
1312  
1313  
1314 sub tie_hash
1315 1 ▶ {
Subroutine tie_hash: called CC=4 SCAR=13.9
Pod undocumented
1316 1 my ($user_options, @args) = @_;
1317  
1318 1 warn_obsolete( "tie_hash is deprecated. Please use hash instead");
1319  
1320 1 my @new ;
1321 1 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 2 1 3
1322 {
1323 2 my $slot = shift @args;
1324 2 my $hash = shift @args ;
1325  
1326 my $tie_class = $hash->{tie}
1327 2 ▶ or croak "tie_hash: missing tie parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $tie_class = $hash->{"tie"} or croak("tie_hash: missing tie parameter") 2 0 2
1328 2 my $tie_args = $hash->{args} ;
1329 2 ▶ my @tie_args = ref $tie_args ? @$tie_args : () ;
Branch: 1 branch
BranchTrueFalseTotal
ref $tie_args ? : 2 0 2
1330  
1331 2 push @new, $slot, { tie_hash => [ $tie_class, @tie_args] };
1332 }
1333  
1334 1 hash($user_options, @new ) ;
1335 }
1336  
1337 sub tie_tie_hash
1338 1 ▶ {
Subroutine tie_tie_hash: called CC=4 SCAR=13.9
Pod undocumented
1339 1 my ($user_options, @args) = @_;
1340  
1341 1 warn_obsolete( "tie_tie_hash is deprecated. Please use hash instead");
1342  
1343 1 my @new ;
1344 1 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 4 1 5
1345 {
1346 4 my $hash = shift @args;
1347 my $slot = delete $hash->{slot}
1348 4 ▶ or croak "No slot names passef to object_tie_hash";
Branch: 1 branch
BranchTrueFalseTotal
my $slot = delete $hash->{"slot"} or croak("No slot names passef to object_tie_hash") 4 0 4
1349  
1350 $hash->{tie_storage} = delete $hash->{tie_scalar}
1351 4 ▶ if defined $hash->{tie_scalar};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{"tie_scalar"} 2 2 4
1352  
1353 4 push @new, $slot, $hash ;
1354 }
1355  
1356 #print Dumper \@new ;
1357 1 hash($user_options, @new ) ;
1358 }
1359  
1360  
1361  
1362  
1363 sub list
1364 2 ▶ {
Subroutine list: called CC=1 SCAR=0.0
Pod undocumented
1365 2 warn_obsolete("list method is obsolete. Please use array");
1366 2 goto &array ;
1367 }
1368  
1369 12 ▶sub _add_array_methods {
Subroutine _add_array_methods: called CC=3 SCAR=11.0
1370 12 my ($methods, $field, $create_array) = @_;
1371  
1372 12 ▶ croak "Create_array is missing" unless defined $create_array ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $create_array 0 12 12
1373  
1374 12 my %stock ;
1375  
1376 $stock{"pop"} =
1377 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1378 3 my ($self) = @_;
1379 3 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 3 3
1380 3 pop @{$self->{$field}}
1381 12 };
1382  
1383 $stock{"push"} =
1384 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1385 5 my ($self, @values) = @_;
1386 5 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 1 4 5
1387 5 push @{$self->{$field}}, @values;
1388 12 };
1389  
1390 $stock{"shift"} =
1391 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1392 4 my ($self) = @_;
1393 4 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 4 4
1394 4 shift @{$self->{$field}}
1395 12 };
1396  
1397 $stock{"unshift"} =
1398 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1399 4 my ($self, @values) = @_;
1400 4 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 4 4
1401 4 unshift @{$self->{$field}}, @values;
1402 12 };
1403  
1404 $stock{"splice"} =
1405 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1406 1 my ($self, $offset, $len, @list) = @_;
1407 1 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 1 1
1408 1 splice(@{$self->{$field}}, $offset, $len, @list);
1409 12 };
1410  
1411 $stock{"clear"} =
1412 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1413 2 my ($self) = @_;
1414 2 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 2 2
1415 2 @{$self->{$field}} = () ;
1416 12 };
1417  
1418 $stock{"count"} =
1419 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1420 2 my ($self) = @_;
1421 2 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 2 2
1422 2 return scalar @{$self->{$field}} ;
1423 12 };
1424  
1425 $stock{"storesize"} =
1426 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1427 1 my ($self,$size) = @_;
1428 1 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 1 0 1
1429 1 $#{$self->{$field}} = $size - 1 ;
1430 12 };
1431  
1432 $stock{"index"} =
1433 41 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1434 41 my $self = shift;
1435 41 my (@indices) = @_;
1436 41 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 1 40 41
1437 41 my @result = @{$self->{$field}}[@_] ;
1438 41 ▶ return $result[0] if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 40 1 41
1439 1 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
1440 12 };
1441  
1442 $stock{set} =
1443 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1444 4 my $self = shift;
1445 4 my @args = @_;
1446 4 ▶ croak "${field}_set expects an even number of fields\n"
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 3 4
1447 if @args % 2;
1448 3 ▶ &$create_array($self,$field) unless defined $self->{$field} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} 0 3 3
1449 3 ▶ while ( my ($index, $value) = splice @args, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 3 3 6
1450 3 $self->{$field}->[$index] = $value;
1451 }
1452 3 return @_ ;#/ 2; # required for object_list
1453 12 };
1454  
1455 12 foreach my $op (keys %stock)
1456 {
1457 120 my $meth = $stock{$op} ;
1458 120 $methods->{$field.'_'.$op} = $meth ;
1459 $methods->{$op.'_'.$field} = sub
1460 13 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
1461 13 warn_obsolete("${op}_$field method is obsolete. Please use ${field}_$op");
1462 13 $meth->(@_) ;
1463 120 } ;
1464 }
1465 }
1466  
1467 =head2 array
1468  
1469 Creates several methods for dealing with slots containing array data.
1470  
1471 array =>
1472 [
1473 'plain_array1', 'plain_array2',
1474 [qw/lot of plain arrayes/] ,
1475 yet_another_plain_array => {} ,
1476  
1477 my_tied_array => {tied_array => 'My_Tie_Array' },
1478 my_tied_array_with_args =>
1479 { tied_array => [ 'My_Tie_Array' , @my_args ] },
1480  
1481 my_array_with_tied_storage => { tie_storage => 'MyTieScalar' },
1482 [qw/likewise_with_args likewise_with_other_args/] =>
1483 { tie_storage => [ 'MyTieScalar', @my_args] }
1484  
1485 my_tied_array_with_tied_storage =>
1486 { tied_array => 'My_Tie_Array',tie_storage => 'MyTieScalar' },
1487  
1488 my_array_with_object => { class_storage => 'MyClass' },
1489 my_array_with_object_and_constructor_args =>
1490 { class_storage => [ 'MyClass' , @my_args ] },
1491  
1492 ]
1493  
1494 The C<array> parameters are:
1495  
1496 =over
1497  
1498 =item *
1499  
1500 A string or a a reference to an array of strings. For each
1501 of these string, a array based slot is created.
1502  
1503 =item *
1504  
1505 A array ref who contains attributes attached to the slot(s) defined by
1506 the previous arguments. These attribute are used to specify the
1507 behavior of the array attached to the slot or to specialize the array
1508 values. See L<Tie::Array::CustomStorage> for details on the possible
1509 attributes.
1510  
1511 =back
1512  
1513 For each slot defined, creates:
1514  
1515 =over 4
1516  
1517 =item x
1518  
1519 This method returns the list of values stored in the slot. In an array
1520 context it returns them as an array and in a scalar context as a
1521 reference to the array. If any arguments are provided to this method,
1522 they I<replace> the current list contents.
1523  
1524 =item x_push
1525  
1526 =item x_pop
1527  
1528 =item x_shift
1529  
1530 =item x_unshift
1531  
1532 =item x_splice
1533  
1534 =item x_clear
1535  
1536 =item x_count
1537  
1538 Returns the number of elements in x.
1539  
1540 =item x_index
1541  
1542 Takes a list of indices, returns a list of the corresponding values.
1543  
1544 =item x_set
1545  
1546 Takes a list, treated as pairs of index => value; each given index is
1547 set to the corresponding value. No return.
1548  
1549 =back
1550  
1551 =cut
1552  
1553 sub array
1554 8 ▶ {
Subroutine array: called CC=9 SCAR=23.3
Pod documented
1555 8 my ($user_options, @args) = @_;
1556 8 my %methods;
1557  
1558 #print "array called with\n", Dumper $user_options, Dumper \@args ;
1559  
1560 8 require Tie::Array::CustomStorage ;
1561  
1562 8 my $parent_method_closure = $user_options->{provide_parent_method} ;
1563  
1564 8 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 10 8 18
1565 {
1566 10 my $hash = shift @args ;
1567 10 ▶ my @slot_hash = ( ref($hash) eq 'ARRAY' ) ? @$hash : ($hash);
Branch: 1 branch
BranchTrueFalseTotal
ref $hash eq 'ARRAY' ? : 2 8 10
1568  
1569 10 ▶ my $x_parm = ref $args[0] ? shift @args : undef ;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 7 3 10
1570 10 my $init_meth = $user_options->{object_init_method} ;
1571 my $create_array = sub
1572 11 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.1
1573 11 my ($self,$name) = @_ ;
1574 11 my @array ;
1575 11 ▶ if (defined $x_parm)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm) 9 2 11
1576 {
1577 my $init_obj = sub
1578 24 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
1579 24 my ($l_obj,$l_idx) = @_ ;
1580 24 ▶ graft_parent_method($l_obj,$self,$name,$l_idx)
Branch: 1 branch
BranchTrueFalseTotal
if $parent_method_closure 7 17 24
1581 if $parent_method_closure ;
1582 24 $child_init->($l_obj, $init_meth) ;
1583 9 } ;
1584  
1585 #print $name,':', Dumper $x_parm ;
1586 9 my $custom_tied_obj = tie @array, 'Tie::Array::CustomStorage', %$x_parm,
1587 init_object => $init_obj ;
1588  
1589 9 ▶ my $user_tied_obj = $custom_tied_obj->get_user_tied_array_object
Branch: 1 branch
BranchTrueFalseTotal
if defined $custom_tied_obj 9 0 9
1590 if defined $custom_tied_obj;
1591 9 ▶ graft_parent_method($user_tied_obj,$self,$name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $user_tied_obj and $parent_method_closure 3 6 9
Condition100%3/3
defined $user_tied_obj and $parent_method_closure
!ll&&!rl&&r
243
MC/DC100%2/2
defined $user_tied_obj and $parent_method_closure
defined $user_tied_obj $parent_method_closure
Truth table100%3/3
A and B
A= defined $user_tied_objB= $parent_method_closure
ABresult
0X0
100
111
1592 if defined $user_tied_obj and $parent_method_closure ;
1593 }
1594 11 $self->{$name} = \@array ;
1595 10 };
1596  
1597 my $handle_value = sub
1598 0 ▶ {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1599 0 my ($self,$name,$key) = splice @_,0,3 ;
1600 0 ▶ return undef unless defined $key ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
1601  
1602 #print "assigning $_[0]\n";
1603 0 ▶ $self->{$name}[$key] = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
1604 0 ▶ return @_ ? $_[0] : $self->{$name}[$key];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1605 10 } ;
1606  
1607 10 foreach my $obj_def (@slot_hash)
1608 {
1609 12 my $name = $obj_def; # kept for closures
1610  
1611 $methods{$name} = sub
1612 30 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
1613 30 my $self = shift ;
1614  
1615 30 ▶ &$create_array($self,$name) unless defined $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$name} 8 22 30
1616  
1617 30 ▶ @{$self->{$name}} = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 24 30
1618 30 ▶ return wantarray ? @{$self->{$name}} : $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 24 30
1619 12 };
1620  
1621 my $tied_array_sub = sub
1622 1 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.2
1623 1 my $self = shift ;
1624 1 ▶ $create_array->($self,$name) unless defined $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$name} 0 1 1
1625 1 my $custom_tied_obj = tied(@{$self->{$name}}) ;
1626 1 ▶ return undef unless defined $custom_tied_obj ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $custom_tied_obj 0 1 1
1627 1 return $custom_tied_obj->get_user_tied_array_object ;
1628 12 } ;
1629  
1630 12 ▶ if (defined $x_parm and defined $x_parm->{tie_array})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm and defined $x_parm->{"tie_array"}) 7 5 12
Condition100%3/3
defined $x_parm and defined $x_parm->{"tie_array"}
!ll&&!rl&&r
327
MC/DC100%2/2
defined $x_parm and defined $x_parm->{"tie_array"}
defined $x_parm defined $x_parm->{"tie_array"}
Truth table100%3/3
A and B
A= defined $x_parmB= defined $x_parm->{"tie_array"}
ABresult
0X0
100
111
1631 {
1632 7 $methods{"tied_array_$name"} = $tied_array_sub ;
1633  
1634 $methods{"tied_$name"} =
1635 sub
1636 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
1637 1 warn_obsolete( "method tied_$name is deprecated") ;
1638 1 return $tied_array_sub->(@_) ;
1639 7 } ;
1640 }
1641  
1642 my $tied_storage_sub = sub
1643 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1644 0 my $self = shift ;
1645 0 my $idx = shift ;
1646 0 ▶ &$create_array($self,$name) unless defined $self->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$name} 0 0 0
1647 0 &$handle_value($self,$name,$idx) ;
1648 0 my $ref = $self->{$name} ;
1649 0 return tied(@$ref)->get_tied_storage_object($idx) ;
1650 12 } ;
1651  
1652 12 ▶ if (defined $x_parm and defined $x_parm->{tie_storage})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x_parm and defined $x_parm->{"tie_storage"}) 0 12 12
Condition66%2/3
defined $x_parm and defined $x_parm->{"tie_storage"}
!ll&&!rl&&r
390
MC/DC0%0/2
defined $x_parm and defined $x_parm->{"tie_storage"}
defined $x_parm defined $x_parm->{"tie_storage"}
Truth table66%2/3
A and B
A= defined $x_parmB= defined $x_parm->{"tie_storage"}
ABresult
0X0
100
111
1653 {
1654 0 $methods{"tied_storage_$name"} = $tied_storage_sub ;
1655 $methods{"tied_scalar_$name"} = sub
1656 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1657 0 warn_obsolete( "method tied_scalar_$name is deprecated") ;
1658 0 return $tied_storage_sub->(@_) ;
1659 0 } ;
1660 }
1661  
1662 12 my @info = get_extended_info($x_parm) ;
1663  
1664 store_slot_in_catalog($name, $user_options->{catalog_name},
1665 12 slot_type => 'array', @info );
1666  
1667 12 _add_array_methods(\%methods, $name, $create_array);
1668 }
1669 }
1670 8 install_methods(%methods);
1671 }
1672  
1673  
1674 sub tie_list
1675 2 ▶ {
Subroutine tie_list: called CC=2 SCAR=6.9
Pod undocumented
1676 2 my ($user_options, @args) = @_;
1677 2 warn_obsolete( "tie_list is deprecated. Please use array instead");
1678  
1679 2 my @new ;
1680 2 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 3 2 5
1681 {
1682 3 my $slot = shift @args;
1683 3 my $tie_args = shift @args ;
1684  
1685 3 push @new, $slot, { tie_array => $tie_args };
1686 }
1687  
1688 #print Dumper \@new ;
1689 2 array($user_options, @new ) ;
1690 }
1691  
1692  
1693 sub object_list
1694 2 ▶ {
Subroutine object_list: called CC=5 SCAR=16.1
Pod undocumented
1695 2 my ($user_options, @args) = @_;
1696 2 warn_obsolete( "tie_list is deprecated. Please use array instead");
1697  
1698 2 my @new ;
1699 2 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 2 2 4
1700 {
1701 2 my $class = shift @args;
1702 2 my $item = shift @args ;
1703  
1704 2 ▶ my $slot = ref $item ? delete $item->{slot} : $item
Branch: 2 branches
BranchTrueFalseTotal
my $slot = ref $item ? delete $item->{"slot"} : $item or croak("object_list: missing slot parameter") 2 0 2
ref $item ? : 1 1 2
1705 or croak "object_list: missing slot parameter";
1706  
1707 2 ▶ my @other = ref $item ? %$item : () ;
Branch: 1 branch
BranchTrueFalseTotal
ref $item ? : 1 1 2
1708 2 push @new, $slot, { class_storage => $class, @other };
1709 }
1710  
1711 #print Dumper \@new ;
1712 2 array($user_options, @new ) ;
1713 }
1714  
1715  
1716 sub object_tie_list
1717 2 ▶ {
Subroutine object_tie_list: called CC=3 SCAR=11.0
Pod undocumented
1718 2 my ($user_options, @args) = @_;
1719 2 warn_obsolete( "object_tie_list is deprecated. Please use array instead");
1720  
1721 2 my @new ;
1722 2 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 2 2 4
1723 {
1724 2 my $h = shift @args ;
1725  
1726 my $slot = delete $h->{slot}
1727 2 ▶ or croak "object_tie_list: missing slot parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $slot = delete $h->{"slot"} or croak("object_tie_list: missing slot parameter") 2 0 2
1728  
1729 2 $h->{class_storage} = delete $h->{class} ;
1730  
1731 2 push @new, $slot, $h;
1732 }
1733  
1734 #print Dumper \@new ;
1735 2 array($user_options, @new ) ;
1736 }
1737  
1738  
1739 =head1 EXAMPLES
1740  
1741 =head2 Creating an object tree
1742  
1743 You can simply create an object with Class::IntrospectionMethods using
1744 a CIMed class in an C<object*> method. For instance, if you want to
1745 create a model of a school clas and their students, you can write:
1746  
1747 Package School_class;
1748  
1749 use Class::IntrospectionMethods
1750 get_set => 'grade',
1751 hash =>
1752 [
1753 student => { class_storage => 'Student'}
1754 ],
1755 new => 'new' ;
1756  
1757 And here is the declaration of the Student class that is used in the
1758 C<School_class> declararion :
1759  
1760 Package Student ;
1761 use Class::IntrospectionMethods
1762 get_set => 'age',
1763 new => 'new' ;
1764  
1765 Now you can use these lines to get and set the student attributes:
1766  
1767 my $son_class = School_class->new ;
1768 $son_class->grade('first') ;
1769 $son_class->student('Ginger')->age(22) ;
1770  
1771 my $ginger = $son_class->student('Ginger') ;
1772 print $ginger->age ;
1773  
1774 =head1 BUGS
1775  
1776 Z<>
1777  
1778 =head1 REPORTING BUGS
1779  
1780 Email the author.
1781  
1782 =head1 THANKS
1783  
1784 To Martyn J. Pearce for C<Class::MethodMaker> and the enlightening
1785 discussion we had a while ago about parent and catalog.
1786  
1787 To Matthew Simon Cavalletto for the parameter translation idea that I
1788 pilfered from C<Class::MakeMethods>.
1789  
1790 =head1 AUTHOR
1791  
1792 Current Maintainer: Dominique Dumont domi@komarr.grenoble.hp.com
1793  
1794 Original Authors: Martyn J. Pearce fluffy@cpan.org, Peter Seibel (Organic Online)
1795  
1796 Contributions from:
1797  
1798 Evolution Online Systems, Inc. http://www.evolution.com
1799 Matthew Persico
1800 Yitzchak Scott-Thoennes
1801  
1802 =head1 COPYRIGHT
1803  
1804 Copyright (c) 2004 Dominique Dumont. This program is free
1805 software; you can redistribute it and/or modify it under the same terms as
1806 Perl itself.
1807  
1808 Copyright (c) 2002, 2001, 2000 Martyn J. Pearce. This program is free
1809 software; you can redistribute it and/or modify it under the same terms as
1810 Perl itself.
1811  
1812 Copyright 1998, 1999, 2000 Evolution Online Systems, Inc. You may use
1813 this software for free under the terms of the MIT License. More info
1814 posted at http://www.evolution.com, or contact info@evolution.com
1815  
1816 Copyright (c) 1996 Organic Online. All rights reserved. This program is
1817 free software; you can redistribute it and/or modify it under the same
1818 terms as Perl itself.
1819  
1820 =head1 SEE ALSO
1821  
1822 C<Class::Struct>, C<Class::MakeMethods>, C<Class::MethodMaker>,
1823 "Object-Oriented Perl" by Damian
1824 Conway. C<Tie::Hash::CustomStorage>, C<Tie::Array::CustomStorage>,
1825 C<Class::IntrospectionMethods::Parent>,
1826 C<Class::IntrospectionMethods::Catalog>
1827  
1828 =cut
↑ Summary IntrospectionMethods/Catalog.pm »