lib/UR/ModuleBase.pm

filter: Statementstmt 41.2% 128 / 310 Branchbran 11.4% 13 / 114 Conditioncond 13.3% 4 / 30 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 48.8% 22 / 45 Podpod 100% 8 / 8 totaltotal 33.6% 177 / 526 CC 350 SCAR 37.7
CC
350
Cov
32%
__ANON__
110.0
debug_message
110.0
error_message
110.0
CRAP 43.5
« lib/UR/DBI.pm ↑ Summary lib/UR/ModuleConfig.pm »
1 # A base class supplying error, warning, status, and debug facilities.
2  
3 package UR::ModuleBase;
4  
5 266 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 266 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 266 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 266 ▶ use Class::Autouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # the file above now does this, but just in case:
11 # subsequent uses of this module w/o the special override should just do nothing...
12 266 $INC{"Class/Autouse_1_99_02.pm"} = 1;
13 266 $INC{"Class/Autouse_1_99_04.pm"} = 1;
14 266 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 # ensure that modules which inherit from this never fall into the
18 # replaced UNIVERSAL::can/isa
19 266 *can = $Class::Autouse::ORIGINAL_CAN;
20 266 *isa = $Class::Autouse::ORIGINAL_ISA;
21 }
22  
23 =pod
24  
25 =head1 NAME
26  
27 UR::ModuleBase - Methods common to all UR classes and object instances.
28  
29 =head1 DESCRIPTION
30  
31 This is a base class for packages, classes, and objects which need to
32 manage basic functionality in the UR framework such as inheritance,
33 AUTOLOAD/AUTOSUB methods, error/status/warning/etc messages.
34  
35 UR::ModuleBase is in the @ISA list for UR::Object, but UR::ModuleBase is not
36 a formal UR class.
37  
38 =head1 METHODS
39  
40 =cut
41  
42 # set up package
43 266 require 5.006_000;
44 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 266 our $VERSION = "0.47"; # UR $VERSION;
47  
48 # set up module
49 266 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 266 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 266 ▶use UR::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 =pod
54  
55 =over
56  
57 =item C<class>
58  
59 $class = $obj->class;
60  
61 This returns the class name of a class or an object as a string.
62 It is exactly equivalent to:
63  
64 (ref($self) ? ref($self) : $self)
65  
66 =cut
67  
68 sub class
69 0 ▶{
Subroutine class: not called CC=2 SCAR=17.9
Pod documented
70 0 my $class = shift;
71 0 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
72 0 return $class;
73 }
74  
75 =pod
76  
77 =item C<super_can>
78  
79 $sub_ref = $obj->super_can('func');
80  
81 This method determines if any of the super classes of the C<$obj>
82 object can perform the method C<func>. If any one of them can,
83 reference to the subroutine that would be called (determined using a
84 depth-first search of the C<@ISA> array) is returned. If none of the
85 super classes provide a method named C<func>, C<undef> is returned.
86  
87 =cut
88  
89 sub super_can
90 0 ▶{
Subroutine super_can: not called CC=3 SCAR=24.8
Pod documented
91 0 my $class = shift;
92
93 0 foreach my $parent_class ( $class->parent_classes )
94 {
95 0 my $code = $parent_class->can(@_);
96 0 ▶ return $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
97 }
98 0 return;
99 }
100  
101 =pod
102  
103 =item C<inheritance>
104  
105 @classes = $obj->inheritance;
106  
107 This method returns a depth-first list of all the classes (packages)
108 that the class that C<$obj> was blessed into inherits from. This
109 order is the same order as is searched when searching for inherited
110 methods to execute. If the class has no super classes, an empty list
111 is returned. The C<UNIVERSAL> class is not returned unless explicitly
112 put into the C<@ISA> array by the class or one of its super classes.
113  
114 =cut
115  
116 46267 ▶sub inheritance {
Subroutine inheritance: called CC=5 SCAR=16.6
Pod documented
117 46267 my $self = $_[0];
118 46267 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0462670
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
119 46267 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 46267 46267
120 266 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 46267 my @parent_classes = @{$class . '::ISA'};
122  
123 46267 my @ordered_inheritance;
124 46267 foreach my $parent_class (@parent_classes) {
125 34301 ▶ push @ordered_inheritance, $parent_class, ($parent_class eq 'UR' ? () : inheritance($parent_class) );
Branch: 1 branch
BranchTrueFalseTotal
$parent_class eq 'UR' ? : 0 34301 34301
126 }
127  
128 46267 return @ordered_inheritance;
129 }
130  
131 =pod
132  
133 =item C<parent_classes>
134  
135 MyClass->parent_classes;
136  
137 This returns the immediate parent class, or parent classes in the case
138 of multiple inheritance. In no case does it follow the inheritance
139 hierarchy as ->inheritance() does.
140  
141 =cut
142  
143 sub parent_classes
144 0 ▶{
Subroutine parent_classes: not called CC=3 SCAR=19.6
Pod documented
145 0 my $self = $_[0];
146 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
147 266 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
148 0 my @parent_classes = @{$class . '::ISA'};
149 0 ▶ return (wantarray ? @parent_classes : $parent_classes[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
150 }
151  
152 =pod
153  
154 =item C<base_dir>
155  
156 MyModule->base_dir;
157  
158 This returns the base directory for a given module, in which the modules's
159 supplemental data will be stored, such as config files and glade files,
160 data caches, etc.
161  
162 It uses %INC.
163  
164 =cut
165  
166 sub base_dir
167 0 ▶{
Subroutine base_dir: not called CC=4 SCAR=30.0
Pod documented
168 0 my $self = shift;
169 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
170 0 $class =~ s/\:\:/\//g;
171 0 ▶ my $dir = $INC{$class . '.pm'} || $INC{$class . '.pl'};
Condition0%0/3
$INC{$class . '.pm'} || $INC{$class . '.pl'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$INC{$class . '.pm'} || $INC{$class . '.pl'}
$INC{$class . '.pm'} $INC{$class . '.pl'}
Truth table0%0/3
A || B
A= $INC{$class . '.pm'}B= $INC{$class . '.pl'}
ABresult
1X1
011
000
172 0 ▶ die "Failed to find module $class in \%INC: " . Data::Dumper(%INC) unless ($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
173 0 $dir =~ s/\.p[lm]\s*$//;
174 0 return $dir;
175 }
176  
177 =pod
178  
179 =item methods
180  
181 Undocumented.
182  
183 =cut
184  
185 sub methods
186 0 ▶{
Subroutine methods: not called CC=5 SCAR=28.6
Pod documented
187 0 my $self = shift;
188 0 my @methods;
189 my %methods;
190 0 my ($class, $possible_method, $possible_method_full, $r, $r1, $r2);
191 266 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 0 for $class (reverse($self, $self->inheritance()))
195 {
196 0 print "$class\n";
197 0 for $possible_method (sort grep { not /^_/ } keys %{$class . "::"})
198 {
199 0 $possible_method_full = $class . "::" . $possible_method;
200
201 0 $r1 = $class->can($possible_method);
202 0 ▶ next unless $r1; # not implemented
Branch: 1 branch
BranchTrueFalseTotal
unless $r1 0 0 0
203
204 0 $r2 = $class->super_can($possible_method);
205 0 ▶ next if $r2 eq $r1; # just inherited
Branch: 1 branch
BranchTrueFalseTotal
if $r2 eq $r1 0 0 0
206
207 {
208 0 push @methods, $possible_method_full;
209 0 push @{ $methods{$possible_method} }, $class;
210 }
211 }
212 }
213 0 print Dumper(\%methods);
214 0 return @methods;
215 }
216  
217 =pod
218  
219 =item C<context_return>
220  
221 return MyClass->context_return(@return_values);
222  
223 Attempts to return either an array or scalar based on the calling context.
224 Will die if called in scalar context and @return_values has more than 1
225 element.
226  
227 =cut
228  
229 798 ▶sub context_return {
Subroutine context_return: called CC=4 SCAR=15.9
Pod documented
230 798 my $class = shift;
231 798 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 798 798
232 798 ▶ return @_ if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 798 798
233 798 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 798 798
234 0 my @caller = caller(1);
235 0 Carp::croak("Method $caller[3] on $class called in scalar context, but " . scalar(@_) . " items need to be returned");
236 }
237 798 return $_[0];
238 }
239  
240 =pod
241  
242 =back
243  
244 =head1 C<AUTOLOAD>
245  
246 This package implements AUTOLOAD so that derived classes can use
247 AUTOSUB instead of AUTOLOAD.
248  
249 When a class or object has a method called which is not found in the
250 final class or any derived classes, perl checks up the tree for
251 AUTOLOAD. We implement AUTOLOAD at the top of the tree, and then
252 check each class in the tree in order for an AUTOSUB method. Where a
253 class implements AUTOSUB, it will receive a function name as its first
254 parameter, and it is expected to return either a subroutine reference,
255 or undef. If undef is returned then the inheritance tree search will
256 continue. If a subroutine reference is returned it will be executed
257 immediately with the @_ passed into AUTOLOAD. Typically, AUTOSUB will
258 be used to generate a subroutine reference, and will then associate
259 the subref with the function name to avoid repeated calls to AUTOLOAD
260 and AUTOSUB.
261  
262 Why not use AUTOLOAD directly in place of AUTOSUB?
263  
264 On an object with a complex inheritance tree, AUTOLOAD is only found
265 once, after which, there is no way to indicate that the given AUTOLOAD
266 has failed and that the inheritance tree trek should continue for
267 other AUTOLOADS which might implement the given method.
268  
269 Example:
270  
271 package MyClass;
272 our @ISA = ('UR');
273 ##- use UR;
274
275 sub AUTOSUB
276 {
277 my $sub_name = shift;
278 if ($sub_name eq 'foo')
279 {
280 *MyClass::foo = sub { print "Calling MyClass::foo()\n" };
281 return \&MyClass::foo;
282 }
283 elsif ($sub_name eq 'bar')
284 {
285 *MyClass::bar = sub { print "Calling MyClass::bar()\n" };
286 return \&MyClass::bar;
287 }
288 else
289 {
290 return;
291 }
292 }
293  
294 package MySubClass;
295 our @ISA = ('MyClass');
296
297 sub AUTOSUB
298 {
299 my $sub_name = shift;
300 if ($sub_name eq 'baz')
301 {
302 *MyClass::baz = sub { print "Calling MyClass::baz()\n" };
303 return \&MyClass::baz;
304 }
305 else
306 {
307 return;
308 }
309 }
310  
311 package main;
312
313 my $obj = bless({},'MySubClass');
314 $obj->foo;
315 $obj->bar;
316 $obj->baz;
317  
318 =cut
319  
320 266 our $AUTOLOAD;
321 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=8 SCAR=38.2
322
323 0 my $self = $_[0];
324
325 # The debugger can't see $AUTOLOAD. This is just here for debugging.
326 0 my $autoload = $AUTOLOAD;
327
328 0 $autoload =~ /(.*)::([^\:]+)$/;
329 0 my $package = $1;
330 0 my $function = $2;
331  
332 0 ▶ return if $function eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $function eq "DESTROY" 0 0 0
333  
334 0 ▶ unless ($package) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package) 0 0 0
335 0 Carp::confess("Failed to determine package name from autoload string $autoload");
336 }
337  
338 # switch these to use Class::AutoCAN / CAN?
339 266 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
340 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
341 0 my @classes = grep {$_} ($self, inheritance($self) );
342 0 for my $class (@classes) {
343 0 ▶ if (my $AUTOSUB = $class->can("AUTOSUB"))
Branch: 1 branch
BranchTrueFalseTotal
if (my $AUTOSUB = $class->can("AUTOSUB")) 0 0 0
344 # FIXME The above causes hard-to-read error messages if $class isn't really a class or object ref
345 # The 2 lines below should fix the problem, but instead make other more impoartant things not work
346 #my $AUTOSUB = eval { $class->can('AUTOSUB') };
347 #if ($AUTOSUB) {
348 {
349 0 ▶ if (my $subref = $AUTOSUB->($function,@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subref = &$AUTOSUB($function, @_)) 0 0 0
350 0 goto $subref;
351 }
352 }
353 }
354  
355 0 ▶ if ($autoload and $autoload !~ /::DESTROY$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoload and not $autoload =~ /::DESTROY$/) 0 0 0
Condition0%0/3
$autoload and not $autoload =~ /::DESTROY$/
!ll&&!rl&&r
000
MC/DC0%0/2
$autoload and not $autoload =~ /::DESTROY$/
$autoload not $autoload =~ /::DESTROY$/
Truth table0%0/3
A and not(B)
A= $autoloadB= not $autoload =~ /::DESTROY$/
ABresult
0X0
100
111
356 0 my $subref = \&Carp::confess;
357 0 @_ = ("Can't locate object method \"$function\" via package \"$package\" (perhaps you forgot to load \"$package\"?)");
358 0 goto $subref;
359 }
360 }
361  
362  
363 =pod
364  
365 =head1 MESSAGING
366  
367 UR::ModuleBase implements several methods for sending and storing error, warning and
368 status messages to the user.
369  
370 # common usage
371  
372 sub foo {
373 my $self = shift;
374 ...
375 if ($problem) {
376 $self->error_message("Something went wrong...");
377 return;
378 }
379 return 1;
380 }
381  
382 unless ($obj->foo) {
383 print LOG $obj->error_message();
384 }
385  
386 =head2 Messaging Methods
387  
388 =over 4
389  
390 =item message_types
391  
392 @types = UR::ModuleBase->message_types;
393 UR::ModuleBase->message_types(@more_types);
394  
395 With no arguments, this method returns all the types of messages that
396 this class handles. With arguments, it adds a new type to the
397 list.
398  
399 Standard message types are fatal, error, status, warning, debug and usage.
400  
401 Note that the addition of new types is not fully supported/implemented
402 yet.
403  
404 =back
405  
406 =cut
407  
408 266 my $create_subs_for_message_type; # filled in lower down
409 266 my @message_types = qw(error status warning debug usage fatal);
410 sub message_types
411 266 ▶{
Subroutine message_types: called CC=4 SCAR=19.7
Pod documented
412 266 my $self = shift;
413 266 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 266 266
414 {
415 0 foreach my $msg_type ( @_ ) {
416 0 ▶ if (! $self->can("${msg_type}_message")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("${msg_type}_message")) 0 0 0
417 # This is a new one
418 0 $create_subs_for_message_type->($self, $msg_type);
419 0 push @message_types, $msg_type;
420 }
421 }
422 } else {
423 266 return grep { $self->can($_ . '_message') } @message_types;
424 }
425 }
426  
427  
428 # Most defaults are false
429 266 my %default_messaging_settings;
430 266 $default_messaging_settings{dump_error_messages} = 1;
431 266 $default_messaging_settings{dump_warning_messages} = 1;
432 266 $default_messaging_settings{dump_status_messages} = 1;
433 266 $default_messaging_settings{dump_fatal_messages} = 1;
434  
435 #
436 # Implement error_mesage/warning_message/status_message in a way
437 # which handles object-specific callbacks.
438 #
439 # Build a set of methods for getting/setting/printing error/warning/status messages
440 # $class->dump_error_messages(<bool>) Turn on/off printing the messages to STDERR
441 # error and warnings default to on, status messages default to off
442 # $class->queue_error_messages(<bool>) Turn on/off queueing of messages
443 # defaults to off
444 # $class->error_message("blah"): set an error message
445 # $class->error_message() return the last message
446 # $class->error_messages() return all the messages that have been queued up
447 # $class->error_messages_arrayref() return the reference to the underlying
448 # list messages get queued to. This is the method for truncating the list
449 # or altering already queued messages
450 # $class->error_messages_callback(<subref>) Specify a callback for when error
451 # messages are set. The callback runs before printing or queueing, so
452 # you can alter @_ and change the message that gets printed or queued
453 # And then the same thing for status and warning messages
454  
455 =pod
456  
457 For each message type, several methods are created for sending and retrieving messages,
458 registering a callback to run when messages are sent, controlling whether the messages
459 are printed on the terminal, and whether the messages are queued up.
460  
461 For example, for the "error" message type, these methods are created:
462  
463 =over 4
464  
465 =item error_message
466  
467 $obj->error_message("Something went wrong...");
468 $obj->error_message($format, @list);
469 $msg = $obj->error_message();
470  
471 When called with one or more arguments, it sends an error message to the
472 object. The error_message_callback will be run, if one is registered, and the
473 message will be printed to the terminal. When given a single argument, it will
474 be passed through unmodified. When given multiple arguments, error_message will
475 assume the first is a format string and the remainder are parameters to
476 sprintf. When called with no arguments, the last message sent will be
477 returned. If the message is C<undef> then no message is printed or queued, and
478 the next time error_message is run as an accessor, it will return
479 undef.
480  
481 Note that C<fatal_message()> will throw an exception at the point it appears
482 in the program. This exception, like others, is trappable bi C<eval>.
483  
484 =item dump_error_messages
485  
486 $obj->dump_error_messages(0);
487 $flag = $obj->dump_error_messages();
488  
489 Get or set the flag which controls whether messages sent via C<error_message()>
490 is printed to the terminal. This flag defaults to true for warning and error
491 messages, and false for others.
492  
493 Note that C<fatal_message()> messages and exceptions do not honor the value of
494 C<dump_fatal_messages()>, and always print their message and throw their
495 exception unless trapped with an C<eval>.
496  
497 =item queue_error_messages
498  
499 $obj->queue_error_messages(0);
500 $flag = $obj->queue_error_messages();
501  
502 Get or set the flag which control whether messages send via C<error_message()>
503 are saved into a list. If true, every message sent is saved and can be retrieved
504 with L<error_messages()> or L<error_messages_arrayref()>. This flag defaults to
505 false for all message types.
506  
507 =item error_messages_callback
508  
509 $obj->error_messages_callback($subref);
510 $subref = $obj->error_messages_callback();
511  
512 Get or set the callback run whenever an error_message is sent. This callback
513 is run with two arguments: The object or class error_message() was called on,
514 and a string containing the message. This callback is run before the message
515 is printed to the terminal or queued into its list. The callback can modify
516 the message (by writing to $_[1]) and affect the message that is printed or
517 queued. If $_[1] is set to C<undef>, then no message is printed or queued,
518 and the last recorded message is set to undef as when calling error_message
519 with undef as the argument.
520  
521 =item error_messages
522  
523 @list = $obj->error_messages();
524  
525 If the queue_error_messages flag is on, then this method returns the entire list
526 of queued messages.
527  
528 When called as an instance method, it returns the errors queued only on that
529 object. When called as a class method, it returns the errors queued on that
530 class, all it's subclasses, and all instances of that class or subclasses.
531  
532 =item error_messages_arrayref
533  
534 $listref = $obj->error_messages_arrayref();
535  
536 If the queue_error_messages flag is on, then this method returns a reference to
537 the actual list where messages get queued. This list can be manipulated to add
538 or remove items.
539  
540 =item error_message_source
541  
542 %source_info = $obj->error_message_source
543  
544 Returns a hash of information about the most recent call to error_message.
545 The key "error_message" contains the message. The keys error_package,
546 error_file, error_line and error_subroutine contain info about the location
547 in the code where error_message() was called.
548  
549 =item error_package
550  
551 =item error_file
552  
553 =item error_line
554  
555 =item error_subroutine
556  
557 These methods return the same data as $obj->error_message_source().
558  
559 =back
560  
561 =cut
562  
563 266 our $stderr = \*STDERR;
564 266 our $stdout = \*STDOUT;
565 266 my %message_settings;
566  
567 # This sub creates the settings mutator subs for each message type
568 # For example, when passed in 'error', it creates the subs error_messages_callback,
569 # queue_error_messages, dump_error_messages, etc
570 1596 ▶$create_subs_for_message_type = sub {
Subroutine __ANON__: called CC=7 SCAR=33.7
571 1596 my($self, $type) = @_;
572  
573 1596 ▶ my $class = ref($self) ? $self->class : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 1596 1596
574  
575 0 ▶ my $save_setting = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
576 0 my($self, $name, $val) = @_;
577 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
578 0 $message_settings{ $self->class . '::' . $name . '_by_id' }->{$self->id} = $val;
579 } else {
580 0 $message_settings{ $self->class . '::' . $name } = $val;
581 }
582 1596 };
583 0 ▶ my $get_setting = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
584 0 my($self, $name) = @_;
585 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
586 return exists($message_settings{ $self->class . '::' . $name . '_by_id' })
587 0 ▶ ? $message_settings{ $self->class . '::' . $name . '_by_id' }->{$self->id}
Branch: 1 branch
BranchTrueFalseTotal
exists $message_settings{$self->class . '::' . $name . '_by_id'} ? : 0 0 0
588 : undef;
589 } else {
590 0 return $message_settings{ $self->class . '::' . $name };
591 }
592 1596 };
593  
594 11172 ▶ my $make_mutator = sub {
Subroutine __ANON__: called CC=1 SCAR=5.6
595 11172 my $name = shift;
596 0 ▶ return sub {
Subroutine warning_messages_callback: not called CC=6 SCAR=37.4
597 0 my $self = shift;
598  
599 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
600 # setting the value
601 0 $save_setting->($self, $name, @_);
602  
603 } else {
604 # getting the value
605 0 my $val = $get_setting->($self, $name);
606 0 ▶ if (defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
elsif (ref $self) { } 0 0 0
607 0 return $val;
608  
609 } elsif (ref $self) {
610 # called on an object and no value set, try the class
611 0 return $self->class->$name();
612  
613 } else {
614 # called on a class name
615 0 my @super = $self->inheritance();
616 0 foreach my $super ( @super ) {
617 0 ▶ if (my $super_sub = $super->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $super_sub = $super->can($name)) 0 0 0
618 0 return $super_sub->($super);
619 }
620 }
621 # None of the parent classes implement it, or there aren't
622 # any parent classes
623 0 return $default_messaging_settings{$name};
624 }
625 }
626 11172 };
627 1596 };
628  
629 1596 foreach my $base ( qw( %s_messages_callback queue_%s_messages %s_package
630 %s_file %s_line %s_subroutine )
631 ) {
632 9576 my $method = sprintf($base, $type);
633 9576 my $full_name = $class . '::' . $method;
634  
635 9576 my $method_subref = Sub::Name::subname $full_name => $make_mutator->($method);
636 9576 Sub::Install::install_sub({
637 code => $method_subref,
638 into => $class,
639 as => $method,
640 });
641 }
642  
643 1596 my $should_dump_messages = "dump_${type}_messages";
644 1596 my $dump_mutator = $make_mutator->($should_dump_messages);
645 1596 my @dump_env_vars = map { $_ . uc($should_dump_messages) } ('UR_', 'UR_COMMAND_');
646 0 ▶ my $should_dump_messages_subref = Sub::Name::subname $class . '::' . $should_dump_messages => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Subroutine dump_warning_messages: not called CC=4 SCAR=30.0
647 0 my $self = shift;
648 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 0 0 0
if (@_) 0 0 0
649 0 return $dump_mutator->($self, @_);
650 }
651 0 foreach my $varname ( @dump_env_vars ) {
652 0 ▶ return $ENV{$varname} if (defined $ENV{$varname});
Branch: 2 branches
BranchTrueFalseTotal
if defined $ENV{$varname} 0 0 0
if defined $ENV{$varname} 0 0 0
653 }
654 0 return $dump_mutator->($self);
655 1596 };
656 1596 Sub::Install::install_sub({
657 code => $should_dump_messages_subref,
658 into => $class,
659 as => $should_dump_messages,
660 });
661  
662  
663 1596 my $messages_arrayref = "${type}_messages_arrayref";
664 0 ▶ my $message_arrayref_sub = Sub::Name::subname "${class}::${messages_arrayref}" => sub {
Subroutine error_messages_arrayref: not called CC=2 SCAR=17.9
Subroutine fatal_messages_arrayref: not called CC=2 SCAR=17.9
665 0 my $self = shift;
666 0 my $a = $get_setting->($self, $messages_arrayref);
667 0 ▶ if (! defined $a) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $a) 0 0 0
unless (defined $a) 0 0 0
668 0 $save_setting->($self, $messages_arrayref, $a = []);
669 }
670 0 return $a;
671 1596 };
672 1596 Sub::Install::install_sub({
673 code => $message_arrayref_sub,
674 into => $class,
675 as => $messages_arrayref,
676 });
677  
678 1596 my $array_subname = "${type}_messages";
679 0 ▶ my $array_subref = Sub::Name::subname "${class}::${array_subname}" => sub {
Subroutine usage_messages: not called CC=5 SCAR=34.0
Subroutine debug_messages: not called CC=5 SCAR=34.0
680 0 my $self = shift;
681 0 ▶ my @search = ref($self)
Branch: 2 branches
BranchTrueFalseTotal
ref $self ? : 0 0 0
ref $self ? : 0 0 0
682 ? $self
683 : ( $self, $self->__meta__->subclasses_loaded, $self->is_loaded() );
684 0 my %seen;
685 my @all_messages;
686 0 foreach my $thing ( @search ) {
687 0 ▶ next if $seen{$thing}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{$thing}++ 0 0 0
if $seen{$thing}++ 0 0 0
688 0 my $a = $get_setting->($thing, $messages_arrayref);
689 0 ▶ push @all_messages, $a ? @$a : ();
Branch: 2 branches
BranchTrueFalseTotal
$a ? : 0 0 0
$a ? : 0 0 0
690 }
691 0 return @all_messages;
692 1596 };
693 1596 Sub::Install::install_sub({
694 code => $array_subref,
695 into => $class,
696 as => $array_subname,
697 });
698  
699  
700 1596 my $messageinfo_subname = "${type}_message_source";
701 1596 my @messageinfo_keys = map { $type . $_ } qw( _message _package _file _line _subroutine );
702 0 ▶ my $messageinfo_subref = Sub::Name::subname "${class}::${messageinfo_subname}" => sub {
Subroutine usage_message_source: not called CC=1 SCAR=6.9
Subroutine warning_message_source: not called CC=1 SCAR=6.9
703 0 my $self = shift;
704 0 return map { $_ => $self->$_ } @messageinfo_keys;
705 1596 };
706 1596 Sub::Install::install_sub({
707 code => $messageinfo_subref,
708 into => $class,
709 as => $messageinfo_subname,
710 });
711  
712 # usage messages go to STDOUT, others to STDERR
713 1596 ▶ my $default_fh = $type eq 'usage' ? \$stdout : \$stderr;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'usage' ? : 266 1330 1596
714  
715 1596 my $should_queue_messages = "queue_${type}_messages";
716 1596 my $check_callback = "${type}_messages_callback";
717 1596 ▶ my $message_text_prefix = ($type eq 'status' or $type eq 'usage') ? '' : uc($type) . ': ';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'status' || $type eq 'usage' ? : 532 1064 1596
Condition100%3/3
$type eq "status" or $type eq "usage"
l!l&&r!l&&!r
2662661064
MC/DC100%2/2
$type eq "status" or $type eq "usage"
$type eq "status" $type eq "usage"
Truth table100%3/3
A or B
A= $type eq "status"B= $type eq "usage"
ABresult
1X1
011
000
718 1596 my $message_package = "${type}_package";
719 1596 my $message_file = "${type}_file";
720 1596 my $message_line = "${type}_line";
721 1596 my $message_subroutine = "${type}_subroutine";
722  
723 my $messaging_action = $type eq 'fatal'
724 0 ▶ ? sub { Carp::croak($message_text_prefix . $_[1]) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
725 0 ▶ : sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
726 0 my($self, $msg) = @_;
727 0 ▶ if (my $fh = $self->$should_dump_messages()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->$should_dump_messages) 0 0 0
728 0 ▶ $fh = $$default_fh unless (ref $fh);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh 0 0 0
729  
730 0 $fh->print($message_text_prefix . $msg . "\n");
731 }
732 1596 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'fatal' ? : 266 1330 1596
733  
734 1596 my $logger_subname = "${type}_message";
735 0 ▶ my $logger_subref = Sub::Name::subname "${class}::${logger_subname}" => sub {
Subroutine status_message: not called CC=10 SCAR=47.0
736 0 my $self = shift;
737  
738 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 0 0 0
if (@_) 0 0 0
739 0 my $msg = shift;
740  
741 # if given multiple arguments, assume it's a format string
742 0 ▶ if(@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 0 0 0
if (@_) 0 0 0
743 0 $msg = _carp_sprintf($msg, @_);
744 }
745  
746 0 ▶ defined($msg) && chomp($msg);
Branch: 2 branches
BranchTrueFalseTotal
defined $msg and chomp $msg 0 0 0
defined $msg and chomp $msg 0 0 0
747  
748 # old-style callback registered with error_messages_callback
749 0 ▶ if (my $code = $self->$check_callback()) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $code = $self->$check_callback) 0 0 0
if (my $code = $self->$check_callback) 0 0 0
750 0 ▶ if (ref $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code) { } 0 0 0
751 0 $code->($self, $msg);
752 } else {
753 0 $self->$code($msg);
754 }
755 }
756  
757 # New-style callback registered as an observer
758 # Some non-UR classes inherit from UR::ModuleBase, and can't __signal
759 0 ▶ if ($UR::initialized && $self->can('__signal_observers__')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::initialized and $self->can("__signal_observers__")) 0 0 0
Condition0%0/3
$UR::initialized and $self->can("__signal_observers__")
!ll&&!rl&&r
000
MC/DC0%0/2
$UR::initialized and $self->can("__signal_observers__")
$UR::initialized $self->can("__signal_observers__")
Truth table0%0/3
A and B
A= $UR::initializedB= $self->can("__signal_observers__")
ABresult
0X0
100
111
760 0 $self->__signal_observers__($logger_subname, $msg);
761 }
762  
763 0 $save_setting->($self, $logger_subname, $msg);
764 # If the callback set $msg to undef with "$_[1] = undef", then they didn't want the message
765 # processed further
766 0 ▶ if (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg) 0 0 0
767 0 ▶ if ($self->$should_queue_messages()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$should_queue_messages) 0 0 0
768 0 my $a = $self->$messages_arrayref();
769 0 push @$a, $msg;
770 }
771  
772 0 my ($package, $file, $line, $subroutine) = caller;
773 0 $self->$message_package($package);
774 0 $self->$message_file($file);
775 0 $self->$message_line($line);
776 0 $self->$message_subroutine($subroutine);
777  
778 0 $self->$messaging_action($msg);
779  
780 }
781 }
782  
783 0 return $get_setting->($self, $logger_subname);
784 1596 };
785 1596 Sub::Install::install_sub({
786 code => $logger_subref,
787 into => $class,
788 as => $logger_subname,
789 });
790  
791 # "Register" the message type as a valid signal.
792 1596 $UR::Object::Type::STANDARD_VALID_SIGNALS{$logger_subname} = 1;
793 266 };
794  
795 0 ▶sub _carp_sprintf {
Subroutine _carp_sprintf: not called CC=2 SCAR=17.9
796 0 my $format = shift;
797 0 my @list = @_;
798  
799 # warnings weren't very helpful because they wouldn't tell you who passed
800 # in the "bad" format string
801 0 my $formatted_string;
802 my $warn_msg;
803 {
804 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
805 0 my $msg = $_[0];
806 0 my ($filename, $line) = (caller)[1, 2];
807 0 my $short_msg = ($msg =~ /(.*) at \Q$filename\E line $line./)[0];
808 0 ▶ $warn_msg = ($short_msg || $msg);
Condition0%0/3
$short_msg || $msg
l!l&&r!l&&!r
000
MC/DC0%0/2
$short_msg || $msg
$short_msg $msg
Truth table0%0/3
A || B
A= $short_msgB= $msg
ABresult
1X1
011
000
809 0 };
810 0 $formatted_string = sprintf($format, @list);
811 }
812 0 ▶ if ($warn_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn_msg) 0 0 0
813 0 Carp::carp($warn_msg);
814 }
815  
816 0 return $formatted_string;
817 }
818  
819  
820 # at init time, make messaging subs for the initial message types
821 266 $create_subs_for_message_type->(__PACKAGE__, $_) foreach @message_types;
822  
823  
824 sub _current_call_stack
825 0 ▶{
Subroutine _current_call_stack: not called CC=4 SCAR=30.0
826 0 my @stack = reverse split /\n/, Carp::longmess("\t");
827  
828 # Get rid of the final line from carp, showing the line number
829 # above from which we called it.
830 0 pop @stack;
831  
832 # Get rid any other function calls which are inside of this
833 # package besides the first one. This allows wrappers to
834 # get_message to look at just the external call stack.
835 # (i.e. AUTOSUB above, set_message/get_message which called this,
836 # and AUTOLOAD in UniversalParent)
837 0 ▶ pop(@stack) while ($stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/ && $stack[-2] && $stack[-2] =~ /^\s*(UR::ModuleBase|UR)::/);
Branch: 1 branch
BranchTrueFalseTotal
$stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/ and $stack[-2] and $stack[-2] =~ /^\s*(UR::ModuleBase|UR)::/ and do { pop @stack } 0 0 0
Condition0%0/6
$stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/ and $stack[-2] and $stack[-2] =~ /^\s*(UR::ModuleBase|UR)::/
!ll&&!rl&&r
000
$stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/ and $stack[-2]
!ll&&!rl&&r
000
MC/DC0%0/3
$stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/ and $stack[-2] and $stack[-2] =~ /^\s*(UR::ModuleBase|UR)::/
$stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/ $stack[-2] $stack[-2] =~ /^\s*(UR::ModuleBase|UR)::/
Truth table0%0/4
A and B and C
A= $stack[-1] =~ /^\s*(UR::ModuleBase|UR)::/B= $stack[-2]C= $stack[-2] =~ /^\s*(UR::ModuleBase|UR)::/
ABCresult
0XX0
10X0
1100
1111
838  
839 0 return \@stack;
840 }
841  
842  
843 266 1;
844 __END__
« lib/UR/DBI.pm ↑ Summary lib/UR/ModuleConfig.pm »