| 1 | # BW::Base.pm | ||||||||||||||||||||
| 2 | # Base methods for BW::* modules | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # by Bill Weinman - http://bw.org/ | ||||||||||||||||||||
| 5 | # Copyright (c) 1995-2010 The BearHeart Group, LLC | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # See HISTORY | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | package BW::Base; | ||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | use BW::Constants; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | our $VERSION = "1.4"; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # | ||||||||||||||||||||
| 20 | # Most of the methods in this module, including new() and _init(), | ||||||||||||||||||||
| 21 | # are designed to be inherited. | ||||||||||||||||||||
| 22 | # | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | sub new | ||||||||||||||||||||
| 25 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 26 | 0 | my $c = shift; | |||||||||||||||||||
| 27 | 0 | ▶ | my $class = ref($c) || $c; | ||||||||||||||||||
|
Condition0%0/3
ref $c || $c
MC/DC0%0/2
ref $c || $c
ref $c
$c
Truth table0%0/3
A || B
A= ref $cB= $c
| |||||||||||||||||||||
| 28 | 0 | my $self = {}; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | bless( $self, $class ); | |||||||||||||||||||
| 31 | 0 | ▶ | $self->_init(@_) or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 0 | return $self; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | sub _init | ||||||||||||||||||||
| 36 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_init: not called CC=7 SCAR=40.3
| |||||||||||||||||||||
| 37 | 0 | my $self = shift; | |||||||||||||||||||
| 38 | 0 | my $arg = $_[0]; | |||||||||||||||||||
| 39 | 0 | my $sn = "_init"; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | $self->{me} = ref($self); | |||||||||||||||||||
| 42 | 0 | $self->{version} = $VERSION; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # handle different sorts of arguments | ||||||||||||||||||||
| 45 | 0 | ▶ | if ( ref($arg) eq 'HASH' ) { # hash ref? | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 46 | 0 | foreach my $k ( keys %$arg ) { | |||||||||||||||||||
| 47 | 0 | ▶ | if ( $self->can($k) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | $self->$k( $arg->{$k} ); | |||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||
| 50 | 0 | $self->_error("$sn: no setter for property $k"); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | } elsif ( defined $_[1] ) { # array? (hash but not ref) | ||||||||||||||||||||
| 54 | 0 | ▶ | while (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | my $k = shift; | |||||||||||||||||||
| 56 | 0 | my $v = shift; | |||||||||||||||||||
| 57 | 0 | ▶ | if ( $self->can($k) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | $self->$k($v); | |||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||
| 60 | 0 | $self->_error("$sn: no setter for property $k"); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | return SUCCESS; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # settergetterers | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | ### generalized setter/getter that is called by all the others | ||||||||||||||||||||
| 71 | sub _setter_getter | ||||||||||||||||||||
| 72 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_setter_getter: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||||||||||
| 74 | 0 | my $arg = shift; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # take the name of the caller and use it as the name of the | ||||||||||||||||||||
| 77 | # property to be set/get | ||||||||||||||||||||
| 78 | 0 | my $caller = ( split( /::/, ( caller(1) )[3] ) )[-1]; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | if ( defined $arg ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | $self->{$caller} = $arg; | |||||||||||||||||||
| 82 | 0 | return SUCCESS; | |||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||
| 84 | 0 | return $self->{$caller}; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # _setter_getter entry points look like this: | ||||||||||||||||||||
| 89 | # sub foo { _setter_getter(@_); } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # debug(m) prints a stack trace along with a debug message | ||||||||||||||||||||
| 92 | sub debug | ||||||||||||||||||||
| 93 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
debug: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 94 | 0 | my ( $self, $message ) = @_; | |||||||||||||||||||
| 95 | 0 | my $out = ''; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my @c = caller(1); | |||||||||||||||||||
| 98 | 0 | ▶ | if ( substr( $c[3], 0, 5 ) eq 'main:' ) { @c = caller(2) } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | my ( $package, $filename, $line, $subroutine, $hashargs, $wantarray, $evaltext, $is_require, $hints, $bitmask ) = @c; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | $message = $self unless ref($self); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | $out .= "$$: "; | |||||||||||||||||||
| 104 | 0 | ▶ | $out .= "$subroutine: " if $subroutine; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | ▶ | $out .= "$message\n" if $message; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | STDERR->print($out); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | # set the error string and return FAILURE | ||||||||||||||||||||
| 110 | sub _error | ||||||||||||||||||||
| 111 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||
| 113 | 0 | ▶ | $self->{error} .= "\n" if $self->{error}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 0 | ▶ | $self->{error} = '' unless $self->{error}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | ▶ | $self->{error} .= "$self->{me}: " . ( shift || 'unknown error' ); | ||||||||||||||||||
|
Condition0%0/2
shift() || 'unknown error'
MC/DC0%0/1
shift() || 'unknown error'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 116 | 0 | return FAILURE; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | # get and clear error string | ||||||||||||||||||||
| 120 | sub error | ||||||||||||||||||||
| 121 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||||||||||
| 123 | 0 | my $errstr = $self->{error}; | |||||||||||||||||||
| 124 | 0 | $self->{error} = VOID; | |||||||||||||||||||
| 125 | 0 | return $errstr; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # check error from other BW::* module | ||||||||||||||||||||
| 129 | sub checkerror | ||||||||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
checkerror: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 131 | 0 | my ( $self, $o ) = @_; | |||||||||||||||||||
| 132 | 0 | my $sn = 'check_error'; | |||||||||||||||||||
| 133 | 0 | ▶ | if ($o) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | my $e = $o->error; | |||||||||||||||||||
| 135 | 0 | ▶ | return $e ? $self->_error($e) : SUCCESS; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||
| 137 | 0 | return $self->_error("$sn: no or unblessed object"); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 1 | 1; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||