File Coverage

blib/lib/App/Framework/Base/Object.pm
Criterion Covered Total %
statement 496 792 62.6
branch 289 378 76.4
condition 23 67 34.3
subroutine 180 277 64.9
pod 87 251 34.6
total 1075 1765 60.9


line stmt bran cond sub pod time code
1             package App::Framework::Base::Object ;
2              
3             =head1 NAME
4              
5             Object - Basic object
6              
7             =head1 SYNOPSIS
8              
9             use App::Framework::Base::Object ;
10              
11              
12             =head1 DESCRIPTION
13              
14              
15             =head1 DIAGNOSTICS
16              
17             Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
18              
19             =head1 AUTHOR
20              
21             Steve Price C<< >>
22              
23             =head1 BUGS
24              
25             None that I know of!
26              
27             =head1 INTERFACE
28              
29             =over 4
30              
31             =cut
32              
33 33     33   25627 use strict ;
  33         76  
  30         932  
34 30     30   140 use Carp ;
  30         50  
  30         1590  
35 30     30   171 use Cwd ;
  30         58  
  30         2679  
36              
37             our $VERSION = "2.002" ;
38             our $AUTOLOAD ;
39              
40             #============================================================================================
41             # USES
42             #============================================================================================
43              
44 30     30   19745 use App::Framework::Base::Object::DumpObj ;
  30         72  
  30         109513  
45              
46             #============================================================================================
47             # GLOBALS
48             #============================================================================================
49             my $global_debug = 0 ;
50             my $global_verbose = 0 ;
51             my $strict_fields = 0 ;
52              
53             my @SPECIAL_FIELDS = qw/
54             global_debug
55             global_verbose
56             strict_fields
57             / ;
58              
59             my %COMMON_FIELDS = (
60             'debug' => undef, # pseudo field
61             'verbose' => undef, # pseudo field
62             ) ;
63              
64             # Constant
65             #my @REQ_LIST ;
66             my %FIELD_LIST ;
67              
68             my %CLASS_INIT;
69             my %CLASS_INSTANCE ;
70              
71             my %DEBUG ;
72             my %VERBOSE ;
73              
74             #============================================================================================
75             # CONSTRUCTOR
76             #============================================================================================
77              
78             =item B
79              
80             Create a new object.
81              
82             The %args are specified as they would be in the B method, for example:
83              
84             'mmap_handler' => $mmap_handler
85              
86             Special arguments are:
87              
88             'fields' => Either ARRAY list of valid field names, or HASH of field names with default values
89              
90             Example:
91              
92             new(
93             'fields' => {
94             'cmd' => undef,
95             'status' => 0,
96             'results' => [],
97            
98             )
99             )
100              
101             All defined fields have an accessor method created.
102              
103             =cut
104              
105             sub new
106             {
107 162     162 1 1328 my ($obj, %args) = @_ ;
108              
109 162   33     1708 my $class = ref($obj) || $obj ;
110             #my $class = $obj->class() ;
111              
112 162 100       651 print "== Object: Creating new $class object ========\n" if $global_debug ;
113 162 50       602 prt_data("ARGS=", \%args, "\n") if $global_debug>=2 ;
114              
115             # Initialise class variables
116 162         1769 $class->init_class(%args);
117              
118             # Create object
119 162         903 my $this = {} ;
120 162         895 bless ($this, $class) ;
121              
122             # Initialise object
123 162         2346 $this->init(%args) ;
124              
125             # # Check for required settings
126             # foreach (@REQ_LIST)
127             # {
128             # do
129             # {
130             # croak "ERROR: $class : Must specify setting for $_" ;
131             # } unless defined($this->{$_}) ;
132             # }
133              
134 162 50       534 prt_data("== Created object=", $this, "================================================\n") if $global_debug ;
135            
136 162         795 return($this) ;
137             }
138              
139             #-----------------------------------------------------------------------------
140              
141             =item B
142              
143             Initialises the newly created object instance.
144              
145              
146             =cut
147              
148             sub init
149             {
150 162     162 1 519 my $this = shift ;
151 162         1153 my (%args) = @_ ;
152              
153 162 50       580 prt_data("init() ARGS=", \%args, "\n") if $global_debug>=3 ;
154              
155             #my $class = $this->class() ;
156             ##my $class = ref($this) || $this ;
157 162         2489 $this = $this->check_instance() ;
158            
159             # Defaults
160             ## my %field_list = $this->field_list() ;
161 162   33     874 my $class = ref($this) || $this ;
162 162         601 my %field_list = ();
163 162 50       636 %field_list = %{ $FIELD_LIST{$class} } if exists($FIELD_LIST{$class}) ;
  162         2169  
164            
165              
166             # May have default value for some or all fields
167 162         1567 my %field_copy ;
168 162         1131 foreach my $fld (keys %field_list)
169             {
170 3319         4552 my $val = $field_list{$fld} ;
171            
172             # If value is an ARRAY ref or a HASH ref then we want a new copy of this per instance (otherwise
173             # all instances will have a ref to the same HASH/ARRAY and one instance will change all instance's values!)
174 3319 100       9139 if (ref($val) eq 'ARRAY')
    100          
175             {
176 987         2042 $val = [@$val] ;
177             }
178             elsif (ref($val) eq 'HASH')
179             {
180 441         1319 $val = { (%$val) } ;
181             }
182            
183 3319         7409 $field_copy{$fld} = $val ;
184             }
185              
186 162         3355 $this->set(%field_copy) ;
187              
188             ## Handle special fields
189 162         1570 foreach my $special (@SPECIAL_FIELDS)
190             {
191 486 50       1630 if (exists($args{$special}))
192             {
193             # remove from args list
194 0         0 my $special_val = delete $args{$special} ;
195            
196             # call variable handler
197 0         0 $this->$special($special_val) ;
198             }
199             }
200              
201             ## Set fields from parameters
202 162         795 $this->set(%args) ;
203              
204 162 50       1607 print "init() - done\n" if $global_debug>=3 ;
205              
206             }
207              
208             #-----------------------------------------------------------------------------
209              
210             =item B
211              
212             Initialises the object class variables.
213              
214              
215             =cut
216              
217             sub init_class
218             {
219 162     162 1 399 my $this = shift ;
220 162         989 my (%args) = @_ ;
221              
222             #my $class = $this->class() ;
223 162   33     1846 my $class = ref($this) || $this ;
224              
225 162 50       694 prt_data("init_class() ARGS=", \%args, "\n") if $global_debug>=3 ;
226             #prt_data("init_class() ARGS (LIST)=", \@_, "\n") ;
227              
228 162 100       669 if (!$CLASS_INIT{$class})
229             {
230             # Field list
231 148         707 $FIELD_LIST{$class} = {};
232 148         666 my $fields = delete($args{'fields'}) ;
233              
234 148 50       580 prt_data(" + fields=$fields", $fields, "ARGS=", \%args, "\n") if $global_debug>=4 ;
235             #prt_data(" init_class($class) FIELDS=", $fields, "\n") ;
236              
237 148 50       468 if ($fields)
238             {
239 148 50       537 print " + fields=$fields ref()=", ref($fields), "\n" if $global_debug>=4 ;
240              
241 148         504 my $class_fields_href = {} ;
242            
243             ## Do the fields
244 148 50       2064 if (ref($fields) eq 'ARRAY')
    50          
245             {
246 0         0 $class_fields_href = {
247             (%COMMON_FIELDS),
248 0         0 map {$_ => undef} @$fields
249             } ;
250             }
251             elsif (ref($fields) eq 'HASH')
252             {
253 148         19077 $class_fields_href = {
254             (%COMMON_FIELDS),
255             (%$fields)
256             } ;
257             }
258             else
259             {
260 0         0 $class_fields_href = {
261             (%COMMON_FIELDS),
262             ($fields => undef)
263             } ;
264             }
265            
266 148         903 $FIELD_LIST{$class} = $class_fields_href ;
267             }
268              
269             # create accessors
270 148         1761 my $code = "package $class;\n" ;
271 148         343 foreach my $field (keys %{$FIELD_LIST{$class}})
  148         1477  
272             {
273 3075 100       46016 if (!$class->can($field))
274             {
275 2760         9130 $code .= qq{
276             ## get / set
277             sub $field
278             {
279             my \$this = shift ;
280             \@_ ? \$this->{$field} = \$_[0] # set
281             : \$this->{$field}; # get
282             }
283             };
284             }
285            
286 3075 100       50687 if (!$class->can("undef_$field"))
287             {
288 2779         9577 $code .= qq{
289             ## undefine
290             sub undef_$field
291             {
292             my \$this = shift ;
293            
294             \$this->{$field} = undef ;
295             }
296             };
297             }
298             }
299            
300 148 50       871 print "Created Accessors:\n$code\n" if $global_debug>=4 ;
301            
302 148 100   9 0 825496 eval $code;
  9 100   9 0 1349  
  9 100   6 0 37  
  9 100   9 0 1833  
  9 100   0 0 34  
  6 100   0 0 2002  
  6 100   3 0 23  
  9 50   0 0 1053  
  9 100   196 1 42  
  0 100   409 1 0  
  0 100   364 1 0  
  0 100   57 1 0  
  0 100   142 1 0  
  3 100   1264 0 5  
  3 100   1175 0 10  
  0 100   92 0 0  
  0 50   269 1 0  
  196 100   45 1 459  
  196 100   567 1 1203  
  409 100   896 1 737  
  409 100   232 0 1586  
  364 100   246 0 681  
  364 100   75 0 1556  
  57 100   141 0 193  
  57 100   165 1 652  
  142 100   164 1 358  
  142 100   333 1 1027  
  1264 100   132 0 2131  
  1264 50   334 1 5103  
  1175 100   276 1 1963  
  1175 100   341 0 4407  
  92 100   231 0 261  
  92 100   15 0 584  
  269 100   172 0 750  
  269 100   379 0 1523  
  45 100   92 0 88  
  45 100   52 0 218  
  567 100   48 0 984  
  567 100   71 0 2269  
  896 100   100 0 1542  
  896 100   17 0 3786  
  232 100   49 0 626  
  232 100   260 0 1331  
  246 100   41 0 514  
  246 100   63 0 1421  
  75 100   114 0 233  
  75 100   54 0 583  
  141 100   57 0 307  
  141 100   52 0 726  
  165 100   38 0 403  
  165 100   48 0 1131  
  164 100   274 1 438  
  164 100   450 1 923  
  333 100   246 1 790  
  333 100   67 1 2324  
  132 100   68 1 492  
  132 100   80 1 1037  
  334 100   78 1 773  
  334 100   47 1 1748  
  276 100   360 1 487  
  276 100   307 1 1404  
  341 100   73 1 580  
  341 100   78 1 2209  
  231 100   87 1 460  
  231 100   77 1 1229  
  15 100   53 1 33  
  15 100   226 1 86  
  172 100   234 1 365  
  172 100   398 1 942  
  379 100   103 1 665  
  379 100   104 1 7964  
  92 100   82 1 209  
  92 100   106 1 526  
  52 100   69 0 131  
  52 100   78 0 445  
  48 100   99 0 126  
  48 100   142 0 297  
  71 100   86 0 171  
  71 100   92 0 388  
  100 100   155 0 199  
  100 100   52 0 492  
  17 100   22 1 50  
  17 100   32 1 103  
  49 100   55 1 135  
  49 100   249 1 239  
  260 100   818 1 616  
  260 100   397 1 1212  
  41 100   166 1 143  
  41 100   177 1 366  
  63 100   110 1 179  
  63 100   118 1 451  
  114 100   115 1 225  
  114 50   91 1 598  
  54 100   223 1 207  
  54 100   246 1 407  
  57 100   221 1 227  
  57 100   15 1 495  
  52 100   61 1 222  
  52 100   79 1 452  
  38 100   71 1 155  
  38 100   70 1 401  
  48 100   52 0 130  
  48 100   119 0 328  
  274 100   76 1 541  
  274 100   89 1 1361  
  450 100   73 0 850  
  450 100   112 0 1674  
  246 100   59 0 629  
  246 100   130 0 1098  
  67 100   117 0 192  
  67 100   91 0 573  
  68 100   111 0 193  
  68 100   139 0 494  
  80 100   137 0 215  
  80 100   91 0 604  
  78 100   62 0 215  
  78 100   248 0 553  
  47 100   233 0 154  
  47 100   87 0 454  
  360     49 0 1166  
  360     0 0 2752  
  307     0 0 600  
  307     0 0 2378  
  73     0 0 230  
  73     0 0 486  
  78     0 0 222  
  78     0 0 478  
  87     1 0 248  
  87     17 0 824  
  77     0 0 378  
  77     0 0 549  
  53     0 0 164  
  53     0 0 371  
  226     2 0 390  
  226     31 0 1111  
  234     28 0 400  
  234     27 0 1382  
  398     26 0 633  
  398     21 0 2271  
  103     27 0 251  
  103     26 0 519  
  104     26 0 249  
  104     0 0 617  
  82     17 0 508  
  82     0 0 460  
  106     29 0 247  
  106     27 0 595  
  69     27 0 176  
  69     28 0 407  
  78     26 0 413  
  78     21 0 3848  
  99     19 0 233  
  99     17 0 934  
  142     0 0 297  
  142     0 0 975  
  86     0 0 214  
  86     0 0 840  
  92     0 0 223  
  92     0 0 853  
  155     0 0 325  
  155     0 0 1146  
  52     0 0 163  
  52     0 0 511  
  22     0 0 54  
  22     0 0 119  
  32     0 0 108  
  32     0 0 410  
  55     0 0 1183  
  55     0 0 485  
  249     0 0 621  
  249     0 0 1160  
  818     0 0 1286  
  818     0 0 3189  
  397     0 0 797  
  397     0 0 1999  
  166     0 0 380  
  166     0 0 906  
  177     29 0 310  
  177     1 0 870  
  110     0 0 402  
  110     0 0 3930  
  118     0 0 273  
  118     0 0 675  
  115     0 0 287  
  115     0 0 900  
  91     2 0 211  
  91     0 0 503  
  223     0 0 412  
  223     0 0 1452  
  246     0 0 461  
  246     0 0 1326  
  221     0 0 395  
  221     17 0 1496  
  15     0 0 32  
  15     0 0 86  
  61     0 0 161  
  61     45 0 538  
  79     36 0 227  
  79     1 0 541  
  71     0 0 189  
  71     0 0 478  
  70     0 0 168  
  70     0 0 463  
  52     0 0 152  
  52     26 0 335  
  119     26 0 237  
  119     26 0 1251  
  76     17 0 200  
  76     0 0 704  
  89     0 0 262  
  89     0 0 632  
  73     0 0 216  
  73     0 0 762  
  112     0 0 279  
  112     0 0 701  
  59     0 0 251  
  59     0 0 580  
  130     0 0 282  
  130     0 0 733  
  117     0 0 290  
  117     0 0 770  
  91     0 1 228  
  91     0 1 635  
  111     0 0 240  
  111     0   1453  
  139     0   413  
  139     0   722  
  137     0   275  
  137     0   671  
  91     0   500  
  91     17   579  
  62     26   203  
  62     0   472  
  248     0   556  
  248     26   1523  
  233     283   393  
  233     66   1025  
  87     570   180  
  87     249   447  
  49         80  
  49         198  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         15  
  17         67  
  17         92  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         6  
  2         9  
  31         103  
  31         186  
  28         119  
  28         185  
  27         125  
  27         230  
  26         139  
  26         195  
  21         105  
  21         151  
  27         65  
  27         128  
  26         58  
  26         91  
  26         50  
  26         122  
  0         0  
  0         0  
  17         98  
  17         165  
  0         0  
  0         0  
  29         130  
  29         194  
  27         129  
  27         207  
  27         135  
  27         3109  
  28         129  
  28         179  
  26         105  
  26         192  
  21         107  
  21         124  
  19         62  
  19         159  
  17         83  
  17         115  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  29         78  
  29         110  
  1         2  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         9  
  2         8  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  17         70  
  17         107  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  45         112  
  45         159  
  36         80  
  36         170  
  1         2  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  26         55  
  26         127  
  26         68  
  26         136  
  26         56  
  26         99  
  17         55  
  17         85  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  17         116  
  17         109  
  26         58  
  26         103  
  0         0  
  0         0  
  0         0  
  0         0  
  26         59  
  26         90  
  283         553  
  283         1699  
  66         267  
  66         488  
  570         1124  
  570         3117  
  249         864  
  249         1637  
303 148 50       741 if ($@) {
304 0         0 die "ERROR defining accessors for '$class':"
305             . "\n\t$@\n"
306             . "-----------------------------------------------------\n"
307             . $code;
308             }
309              
310             ## Create private fields
311            
312 148 50       717 prt_data(" init_class: class=$class FIELD_LIST=", \%FIELD_LIST) if $global_debug>=4 ;
313              
314             # Finished
315 148         941 $CLASS_INIT{$class}=1;
316             }
317              
318 162 50       1298 print "init_class() - done\n" if $global_debug>=3 ;
319             }
320              
321             #-----------------------------------------------------------------------------
322              
323             =item B
324              
325             Adds the contents of the HASH ref $fields_href to the args HASH ref ($args_href) under the key
326             'fields'. Used by derived objects to add their fields to the parent object's fields.
327              
328              
329             =cut
330              
331             sub add_fields
332             {
333 475     475 1 1059 my $this = shift ;
334 475         864 my ($fields_href, $args_href) = @_ ;
335              
336             # Add extra fields
337 475         4958 foreach (keys %$fields_href)
338             {
339 2667         10931 $args_href->{'fields'}{$_} = $fields_href->{$_} ;
340             }
341              
342             }
343              
344             #-----------------------------------------------------------------------------
345              
346             =item B
347              
348             Initialises the object class variables. Creates a class instance so that these
349             methods can also be called via the class (don't need a specific instance)
350              
351             =cut
352              
353             sub init_class_instance
354             {
355 0     0 1 0 my $class = shift ;
356 0         0 my (%args) = @_ ;
357              
358 0         0 $class->init_class(%args) ;
359              
360             # Create a class instance object - allows these methods to be called via class
361 0         0 $class->class_instance(%args) ;
362            
363             # Set any global values
364 0         0 $class->set(%args) ;
365             }
366              
367             #----------------------------------------------------------------------------
368             # Return global fields hash
369             sub _field_list
370             {
371 0     0   0 my $class = shift ;
372              
373 0         0 return %FIELD_LIST ;
374             }
375              
376             #============================================================================================
377             # CLASS METHODS
378             #============================================================================================
379              
380             #----------------------------------------------------------------------------
381              
382             =item B
383              
384             Set global debug print options to I.
385              
386             0 = No debug
387             1 = standard debug information
388             2 = verbose debug information
389              
390             =cut
391              
392             sub global_debug
393             {
394 0     0 1 0 my $this = shift ;
395 0         0 my ($flag) = @_ ;
396              
397             #my $class = $this->class() ;
398             ##my $class = ref($this) || $this ;
399              
400 0         0 my $old = $global_debug ;
401 0 0       0 $global_debug = $flag if defined($flag) ;
402              
403 0         0 return $old ;
404             }
405              
406              
407             #----------------------------------------------------------------------------
408              
409             =item B
410              
411             Set global verbose print level to I.
412              
413             0 = None verbose
414             1 = verbose information
415             2 = print commands
416             3 = print command results
417              
418             =cut
419              
420             sub global_verbose
421             {
422 0     0 1 0 my $this = shift ;
423 0         0 my ($flag) = @_ ;
424              
425             #my $class = $this->class() ;
426             ##my $class = ref($this) || $this ;
427              
428 0         0 my $old = $global_verbose ;
429 0 0       0 $global_verbose = $flag if defined($flag) ;
430              
431 0         0 return $old ;
432             }
433              
434             #----------------------------------------------------------------------------
435              
436             =item B
437              
438             Enable/disable strict field checking
439              
440             =cut
441              
442             sub strict_fields
443             {
444 0     0 1 0 my $this = shift ;
445 0         0 my ($flag) = @_ ;
446              
447             #my $class = $this->class() ;
448             ##my $class = ref($this) || $this ;
449              
450 0         0 my $old = $strict_fields ;
451 0 0       0 $strict_fields = $flag if defined($flag) ;
452              
453 0         0 return $old ;
454             }
455              
456             #----------------------------------------------------------------------------
457              
458             =item B
459              
460             Returns an object that can be used for class-based calls - object contains
461             all the usual fields
462            
463             =cut
464              
465             sub class_instance
466             {
467 162     162 1 1704 my $this = shift ;
468 162         900 my (@args) = @_ ;
469              
470             #my $class = $this->class() ;
471 162   33     1886 my $class = ref($this) || $this ;
472              
473 162 100 100     2827 if ($class->allowed_class_instance() && !$class->has_class_instance())
474             {
475 2         7 $CLASS_INSTANCE{$class} = 1 ; # ensure we don't get here again (breaks recursive loop)
476              
477 2 50       16 print "-- Create class instance --\n" if $global_debug>=3 ;
478            
479             # Need to create one using the args
480 2         17 $CLASS_INSTANCE{$class} = $class->new(@args) ;
481             }
482              
483              
484 162         2128 return $CLASS_INSTANCE{$class} ;
485             }
486              
487             #----------------------------------------------------------------------------
488              
489             =item B
490              
491             Returns true if this class has a class instance object
492            
493             =cut
494              
495             sub has_class_instance
496             {
497 5     5 1 10 my $this = shift ;
498             #my $class = $this->class() ;
499 5   33     31 my $class = ref($this) || $this ;
500              
501             #prt_data("has_class_instance($class) CLASS_INSTANCE=", \%CLASS_INSTANCE) if $global_debug>=5 ;
502              
503 5         44 return exists($CLASS_INSTANCE{$class}) ;
504             }
505              
506             #----------------------------------------------------------------------------
507              
508             =item B
509              
510             Returns true if this class can have a class instance object
511            
512             =cut
513              
514             sub allowed_class_instance
515             {
516 5     5 1 75 return 1 ;
517             }
518              
519             #----------------------------------------------------------------------------
520              
521             =item B
522              
523             Returns hash of object's field definitions.
524              
525             =cut
526              
527             sub field_list
528             {
529 0     0 1 0 my $this = shift ;
530              
531             #my $class = $this->class() ;
532 0   0     0 my $class = ref($this) || $this ;
533            
534 0         0 my $href ;
535 0 0       0 $href = $FIELD_LIST{$class} if exists($FIELD_LIST{$class}) ;
536              
537 0 0       0 return $href ? %$href : () ;
538             }
539              
540              
541             #============================================================================================
542             # OBJECT DATA METHODS
543             #============================================================================================
544              
545             #----------------------------------------------------------------------------
546              
547             =item B
548              
549             Set debug print options to I.
550              
551              
552             =cut
553              
554             sub debug
555             {
556 37937     37937 1 46684 my $this = shift ;
557 37937         40383 my ($level) = @_ ;
558              
559             #my $class = $this->class() ;
560 37937   33     81221 my $class = ref($this) || $this ;
561             #print "In debug() for $class\n" ;
562              
563 37937   100     137033 $DEBUG{$class} ||= 0 ;
564 37937         48765 my $old = $DEBUG{$class} ;
565 37937 100       71265 $DEBUG{$class} = $level if defined($level) ;
566              
567 37937         138066 return $old ;
568             }
569              
570             #----------------------------------------------------------------------------
571              
572             =item B
573              
574             Set debug print options flag to undefined.
575              
576              
577             =cut
578              
579             sub undef_debug
580             {
581 162     162 1 419 my $this = shift ;
582 162         305 my ($level) = @_ ;
583              
584             #my $class = $this->class() ;
585 162   33     609 my $class = ref($this) || $this ;
586             #print "In undef_debug() for $class\n" ;
587              
588 162   50     1217 $DEBUG{$class} ||= 0 ;
589 162         308 my $old = $DEBUG{$class} ;
590 162         326 $DEBUG{$class} = undef ;
591              
592 162         475 return $old ;
593             }
594              
595              
596             #----------------------------------------------------------------------------
597              
598             =item B
599              
600             Set verbose print options to I.
601              
602              
603             =cut
604              
605             sub verbose
606             {
607 211     211 1 350 my $this = shift ;
608 211         322 my ($level) = @_ ;
609              
610             #my $class = $this->class() ;
611 211   33     588 my $class = ref($this) || $this ;
612             #print "In verbose() for $class\n" ;
613              
614 211   50     1269 $VERBOSE{$class} ||= 0 ;
615 211         304 my $old = $VERBOSE{$class} ;
616 211 50       535 $VERBOSE{$class} = $level if defined($level) ;
617              
618 211         564 return $old ;
619             }
620              
621             #----------------------------------------------------------------------------
622              
623             =item B
624              
625             Set verbose print options flag to undefined.
626              
627              
628             =cut
629              
630             sub undef_verbose
631             {
632 162     162 1 416 my $this = shift ;
633 162         284 my ($level) = @_ ;
634              
635             #my $class = $this->class() ;
636 162   33     597 my $class = ref($this) || $this ;
637             #print "In undef_verbose() for $class\n" ;
638              
639 162   50     1515 $DEBUG{$class} ||= 0 ;
640 162         313 my $old = $DEBUG{$class} ;
641 162         311 $DEBUG{$class} = undef ;
642              
643 162         475 return $old ;
644             }
645              
646             #----------------------------------------------------------------------------
647              
648             =item B
649              
650             Get/set a field value. Used by derived objects to get/set the underlying object field
651             variable when they have overridden that field's access method.
652              
653             =cut
654              
655             sub field_access
656             {
657 34     34 1 124 my $this = shift ;
658 34         148 my ($field, $value) = @_ ;
659              
660 34   33     212 my $class = ref($this) || $this ;
661 34         101 my %field_list = ();
662 34 50       167 %field_list = %{ $FIELD_LIST{$class} } if exists($FIELD_LIST{$class}) ;
  34         658  
663 34 50       288 $this->throw_fatal("Attempting to access an invalid field \"$field\" for this object class \"$class\" ") unless (exists($field_list{$field})) ;
664              
665 34 100       191 $this->{$field} = $value if defined($value) ;
666 34         268 return $this->{$field} ;
667             }
668              
669              
670              
671              
672              
673             #----------------------------------------------------------------------------
674              
675             =item B
676              
677             Set one or more settable parameter.
678              
679             The %args are specified as a hash, for example
680              
681             set('mmap_handler' => $mmap_handler)
682              
683             Sets field values. Field values are expressed as part of the HASH (i.e. normal
684             field => value pairs).
685              
686             =cut
687              
688             sub set
689             {
690 611     611 1 2030 my $this = shift ;
691 611         3659 my (%args) = @_ ;
692              
693 611 50       2185 prt_data("set() ARGS=", \%args, "\n") if $global_debug>=3 ;
694              
695 611         2475 $this = $this->check_instance() ;
696            
697             # Args
698             ## my %field_list = $this->field_list() ;
699 611   33     2998 my $class = ref($this) || $this ;
700 611         1242 my %field_list = ();
701 611 50       1688 %field_list = %{ $FIELD_LIST{$class} } if exists($FIELD_LIST{$class}) ;
  611         11219  
702              
703 611         4335 foreach my $field (keys %field_list)
704             {
705 16023 100       32189 if (exists($args{$field}))
706             {
707 4616 50       9046 print " + set $field = $args{$field}\n" if $global_debug>=3 ;
708              
709             # Need to call actual method (rather than ___set) so that it can be overridden
710 4616 100       9848 if (!defined($args{$field}))
711             {
712             # Set to undef
713 1068         2957 my $undef_method = "undef_$field" ;
714 1068         24656 $this->$undef_method() ;
715             }
716             else
717             {
718 3548         799649 $this->$field($args{$field}) ;
719             }
720             }
721             }
722              
723             ## See if strict checks are enabled
724 611 50       2733 if ($strict_fields)
725             {
726             # Check to ensure that only the valid fields are being set
727 0         0 foreach my $field (keys %args)
728             {
729 0 0       0 if (!exists($field_list{$field}))
730             {
731 0         0 print "WARNING::Attempt to set invalid field \"$field\" \n" ;
732 0         0 $this->dump_callstack() ;
733             }
734             }
735             }
736            
737 611 50       4943 print "set() - done\n" if $global_debug>=3 ;
738              
739             }
740              
741             #----------------------------------------------------------------------------
742              
743             =item B
744              
745             Returns hash of object's fields (i.e. field name => field value pairs).
746              
747             If @names array is specified, then only returns the HASH containing the named fields.
748              
749             =cut
750              
751             sub vars
752             {
753 211     211 1 355 my $this = shift ;
754 211         887 my (@names) = @_ ;
755              
756             ## my %field_list = $this->field_list() ;
757 211   33     652 my $class = ref($this) || $this ;
758 211         423 my %field_list = ();
759 211 50       611 %field_list = %{ $FIELD_LIST{$class} } if exists($FIELD_LIST{$class}) ;
  211         4998  
760              
761 211         667 my %fields ;
762              
763             #prt_data("vars() names=", \@names) ;
764            
765             # If no names specified then get all of them
766 211 50       674 unless (@names)
767             {
768 211         2178 @names = keys %field_list ;
769             }
770 211         847 my %names = map {$_ => 1} @names ;
  7130         11440  
771             #prt_data(" + names=", \%names) ;
772            
773             # Get the value of each field
774 211         1891 foreach my $field (keys %field_list)
775             {
776             # Store field if we've asked for it
777 7130 50       194768 $fields{$field} = $this->$field() if exists($names{$field}) ;
778             #print " + + $field : " ;
779             #if (exists($fields{$field}))
780             #{
781             # print "ok ($fields{$field})\n" ;
782             #}
783             #else
784             #{
785             # print "not wanted\n" ;
786             #}
787             }
788            
789 211         7997 return %fields ;
790             }
791              
792              
793              
794              
795             #----------------------------------------------------------------------------
796              
797             =item B
798              
799             Destroy object
800              
801             =cut
802              
803             sub DESTROY
804             {
805 6     6   1422 my $this = shift ;
806              
807             }
808              
809              
810             #============================================================================================
811             # OBJECT METHODS
812             #============================================================================================
813              
814             #----------------------------------------------------------------------------
815              
816             =item B
817              
818             If this is not an instance (i.e. a class call), then if there is a class_instance
819             defined use it, otherwise error.
820              
821             =cut
822              
823             sub check_instance
824             {
825 795     795 1 1403 my $this = shift ;
826 795         1637 my (%args) = @_ ;
827              
828             #my $class = $this->class() ;
829            
830 795 50       2478 if (!ref($this))
831             {
832 0   0     0 my $class = ref($this) || $this ;
833 0 0       0 if ($class->has_class_instance())
834             {
835 0         0 $this = $class->class_instance() ;
836             }
837             else
838             {
839 0         0 croak "$this is not a usable object" ;
840             }
841             }
842              
843 795         2146 return $this ;
844             }
845              
846              
847             #----------------------------------------------------------------------------
848              
849             =item B
850              
851             Transfers all the supported attributes from $this object to $target object.
852              
853             =cut
854              
855             sub copy_attributes
856             {
857 0     0 1 0 my $this = shift ;
858 0         0 my ($target) = @_ ;
859              
860 0         0 $this = $this->check_instance() ;
861 0         0 $target = $target->check_instance() ;
862            
863             # Get list of fields in the target
864 0         0 my %target_field_list = $target->field_list() ;
865            
866             # Copy values from this object
867 0         0 my %field_list = $this->field_list() ;
868 0         0 foreach my $field (keys %target_field_list)
869             {
870             # see if can copy
871 0 0       0 if (exists($field_list{$field}))
872             {
873 0         0 $target->set($field => $this->$field()) ;
874             }
875             }
876            
877             }
878              
879             #----------------------------------------------------------------------------
880              
881             =item B
882              
883             Returns name of object class.
884              
885             =cut
886              
887             sub class
888             {
889 76     76 1 145 my $this = shift ;
890              
891 76   33     291 my $class = ref($this) || $this ;
892            
893 76         603 return $class ;
894             }
895              
896             #----------------------------------------------------------------------------
897              
898             =item B
899              
900             Create a copy of this object and return the copy.
901              
902             =cut
903              
904             sub clone
905             {
906 0     0 1 0 my $this = shift ;
907              
908 0         0 my $clone ;
909            
910             # TODO: WRITE IT!
911            
912 0         0 return $clone ;
913             }
914              
915              
916              
917             # ============================================================================================
918             # UTILITY METHODS
919             # ============================================================================================
920              
921              
922              
923             #----------------------------------------------------------------------------
924              
925             =item B
926              
927             Returns a quoted version of the string.
928            
929             =cut
930              
931             sub quote_str
932             {
933 0     0 1 0 my $this = shift ;
934 0         0 my ($str) = @_ ;
935            
936             ##my $class = $this->class() ;
937              
938             # skip on Windows machines
939 0 0       0 unless ($^O eq 'MSWin32')
940             {
941             # first escape any existing quotes
942 0         0 $str =~ s%\\'%'%g ;
943 0         0 $str =~ s%'%'\\''%g ;
944            
945 0         0 $str = "'".$str."'" ;
946             }
947            
948            
949 0         0 return $str ;
950             }
951              
952             #----------------------------------------------------------------------------
953              
954             =item B
955              
956             Work through string expanding any variables, replacing them with the value stored in the %vars hash.
957             If variable is not stored in %vars, then that variable is left.
958              
959             Returns expanded string.
960              
961             =cut
962              
963             sub expand_vars
964             {
965 0     0 1 0 my $this = shift ;
966 0         0 my ($string, $vars_href) = @_ ;
967              
968              
969             # Do replacement
970 0         0 $string =~ s{
971             \$ # find a literal dollar sign
972             \{{0,1} # optional brace
973             (\w+) # find a "word" and store it in $1
974             \}{0,1} # optional brace
975             }{
976 30     30   310 no strict 'refs'; # for $$1 below
  30         54  
  30         39939  
977 0 0       0 if (defined $vars_href->{$1}) {
978 0         0 $vars_href->{$1}; # expand variable
979             } else {
980 0         0 "\${$1}"; # leave it
981             }
982             }egx;
983              
984 0         0 return $string ;
985             }
986              
987              
988              
989             #---------------------------------------------------------------------
990              
991             =item B
992              
993             Use App::Framework::Base::Object::DumpObj to print out variable information. Automatically enables
994             object print out
995            
996             =cut
997              
998             sub prt_data
999             {
1000 1266     1266 1 32831 my $this = shift ;
1001 1266         2511 my (@args) = @_ ;
1002            
1003 1266         3421 App::Framework::Base::Object::DumpObj::print_objects_flag(1) ;
1004 1266         3356 App::Framework::Base::Object::DumpObj::prt_data(@args) ;
1005             }
1006              
1007             #----------------------------------------------------------------------------
1008             #
1009             #=item B<_dbg_prt($items_aref [, $min_debug])>
1010             #
1011             #Print out the items in the $items_aref ARRAY ref iff the calling object's debug level is >0.
1012             #If $min_debug is specified, will only print out items if the calling object's debug level is >= $min_debug.
1013             #
1014             #=cut
1015             #
1016             sub _dbg_prt
1017             {
1018 37417     37417   47378 my $obj = shift ;
1019 37417         44964 my ($items_aref, $min_debug) = @_ ;
1020              
1021 37417   100     82797 $min_debug ||= 1 ;
1022            
1023             ## check debug level setting
1024 37417 100       71226 if ($obj->debug >= $min_debug)
1025             {
1026 1170         1828 my $pkg = ref($obj) ;
1027 1170         3663 $pkg =~ s/App::Framework/ApFw/ ;
1028            
1029 1170         4095 my $prefix = App::Framework::Base::Object::DumpObj::prefix("$pkg :: ") ;
1030 1170         3492 $obj->prt_data(@$items_aref) ;
1031 1170         3160 App::Framework::Base::Object::DumpObj::prefix($prefix) ;
1032             }
1033             }
1034              
1035              
1036              
1037             #---------------------------------------------------------------------
1038              
1039             =item B
1040              
1041             Print out the call stack. Useful for debug output at a crash site.
1042             =cut
1043              
1044             sub dump_callstack
1045             {
1046 12     12 1 21 my $this = shift ;
1047 12         32 my ($package, $filename, $line, $subr, $has_args, $wantarray) ;
1048 12         21 my $i=0 ;
1049 12         183 print "\n-----------------------------------------\n";
1050             do
1051 12         21 {
1052 86         1877 ($package, $filename, $line, $subr, $has_args, $wantarray) = caller($i++) ;
1053 86 100       235 if ($subr)
1054             {
1055 74         1200 print "$filename :: $subr :: $line\n" ;
1056             }
1057             }
1058             while($subr) ;
1059 12         152 print "-----------------------------------------\n\n";
1060             }
1061              
1062              
1063              
1064             # ============================================================================================
1065             # PRIVATE METHODS
1066             # ============================================================================================
1067              
1068             #----------------------------------------------------------------------------
1069             # Set field value
1070             sub ___set
1071             {
1072 0     0   0 my $this = shift ;
1073 0         0 my ($field, $new_value) = @_ ;
1074              
1075             ## NEW
1076 0 0       0 if ($global_debug>=10)
1077             {
1078 0         0 print "Unexpected ___set($field, $new_value)\n" ;
1079 0         0 $this->dump_callstack() ;
1080             }
1081             ## NEW
1082              
1083              
1084             #my $class = $this->class() ;
1085 0         0 my $value ;
1086              
1087             # Check that field name is valid
1088             ## my %field_list = $this->field_list() ;
1089 0   0     0 my $class = ref($this) || $this ;
1090 0         0 my %field_list = ();
1091 0 0       0 %field_list = %{ $FIELD_LIST{$class} } if exists($FIELD_LIST{$class}) ;
  0         0  
1092              
1093 0 0       0 if (!exists($field_list{$field}))
1094             {
1095             ## my $class = ref($this) || $this ;
1096              
1097 0 0       0 prt_data("$class : ___set($field) invalid field. Valid fields=", \%field_list) if $global_debug>=5 ;
1098 0 0       0 $this->dump_callstack() if $global_debug>=10 ;
1099              
1100             # TODO: Do something more useful!
1101 0         0 croak "$class: Attempting to write invalid field $field" ;
1102             }
1103             else
1104             {
1105             # get existing value
1106 0         0 $value = $this->{$field} ;
1107            
1108             # write
1109 0         0 $this->{$field} = $new_value ;
1110             }
1111 0 0       0 print " + ___set($field) <= $new_value (was $value)\n" if $global_debug>=5 ;
1112              
1113             # Return previous value
1114 0         0 return $value ;
1115             }
1116              
1117             #----------------------------------------------------------------------------
1118             # get field value
1119             sub ___get
1120             {
1121 0     0   0 my $this = shift ;
1122 0         0 my ($field) = @_ ;
1123              
1124 0         0 my $value ;
1125            
1126             #my $class = $this->class() ;
1127              
1128             ## NEW
1129 0 0       0 if ($global_debug>=10)
1130             {
1131 0         0 print "Unexpected ___get($field)\n" ;
1132 0         0 $this->dump_callstack() ;
1133             }
1134             ## NEW
1135              
1136              
1137             # Check that field name is valid
1138             ## my %field_list = $this->field_list() ;
1139 0   0     0 my $class = ref($this) || $this ;
1140 0         0 my %field_list = ();
1141 0 0       0 %field_list = %{ $FIELD_LIST{$class} } if exists($FIELD_LIST{$class}) ;
  0         0  
1142              
1143 0 0       0 if (!exists($field_list{$field}))
1144             {
1145             ## my $class = ref($this) || $this ;
1146              
1147 0 0       0 prt_data("$class : ___get($field) invalid field. Valid fields=", \%field_list) if $global_debug>=5 ;
1148 0         0 prt_data("$class : ___get($field) invalid field. Valid fields=", \%field_list) ;
1149 0 0       0 $this->dump_callstack() if $global_debug>=10 ;
1150 0         0 $this->dump_callstack() ;
1151              
1152             # TODO: Do something more useful!
1153 0         0 croak "$class: Attempting to access invalid method $field (or read using invalid data accessor)" ;
1154             }
1155             else
1156             {
1157             # get existing value
1158 0         0 $value = $this->{$field} ;
1159             }
1160              
1161 0 0       0 print " + ___get($field) = $value\n" if $global_debug>=5 ;
1162              
1163             # Return previous value
1164 0         0 return $value ;
1165             }
1166              
1167              
1168             # ============================================================================================
1169              
1170             # Autoload handle only field value set/undefine
1171             # Set method =
1172             # Undefine method = undef_
1173             #
1174             sub AUTOLOAD
1175             {
1176 0 0   0   0 print "AUTOLOAD ($AUTOLOAD)\n" if $global_debug>=5 ;
1177              
1178             ## NEW
1179 0 0       0 if ($global_debug>=10)
1180             {
1181 0         0 my $caller = (caller())[0] ;
1182 0         0 print "Unexpected AUTOLOAD ($AUTOLOAD) from $caller\n" ;
1183             }
1184             ## NEW
1185              
1186 0         0 my $this = shift;
1187             # prt_data("AUTOLOAD ($AUTOLOAD) this=", $this) if $global_debug>=5 ;
1188              
1189             #print "$this=",ref($this),"\n";
1190 0 0 0     0 if (!ref($this)||ref($this)eq'ARRAY')
1191             {
1192 0         0 croak "AUTOLOAD ($AUTOLOAD) (@_): $this is not a valid object" ;
1193             }
1194              
1195 0         0 $this = $this->check_instance() ;
1196             # prt_data(" + this=", $this) if $global_debug>=5 ;
1197              
1198 0         0 my $name = $AUTOLOAD;
1199 0         0 $name =~ s/.*://; # strip fully-qualified portion
1200 0         0 my $class = $AUTOLOAD;
1201 0         0 $class =~ s/::[^:]+$//; # get class
1202              
1203 0         0 my $type = ref($this) ;
1204            
1205             # if (!$type)
1206             # {
1207             # # see if there is a class instance object defined
1208             # if ($class->has_class_instance())
1209             # {
1210             # $this = $class->class_instance() ;
1211             # $type = ref($this) ;
1212             # }
1213             # else
1214             # {
1215             # croak "$this is not an object";
1216             # }
1217             # }
1218              
1219             # possibly going to set a new value
1220 0         0 my $set=0;
1221 0         0 my $new_value = shift;
1222 0 0       0 $set = 1 if defined($new_value) ;
1223            
1224             # 1st see if this is of the form undef_
1225 0 0       0 if ($name =~ m/^undef_(\w+)$/)
1226             {
1227 0         0 $set = 1 ;
1228 0         0 $name = $1 ;
1229 0         0 $new_value = undef ;
1230             }
1231              
1232 0         0 my $value = $this->___get($name);
1233              
1234 0 0       0 if ($set)
1235             {
1236 0         0 $this->___set($name, $new_value) ;
1237             }
1238              
1239             # Return previous value
1240 0         0 return $value ;
1241             }
1242              
1243              
1244              
1245             # ============================================================================================
1246             # END OF PACKAGE
1247             1;
1248              
1249             __END__