File Coverage

blib/lib/BW/Base.pm
Criterion Covered Total %
statement 12 68 17.6
branch 0 28 0.0
condition 0 5 0.0
subroutine 4 11 36.3
pod 4 4 100.0
total 20 116 17.2


line stmt bran cond sub pod time code
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     1   1068 use strict;
  1         3  
  1         41  
12 1     1   6 use warnings;
  1         1  
  1         34  
13 1     1   40 use 5.008;
  1         3  
  1         44  
14              
15 1     1   522 use BW::Constants;
  1         3  
  1         1325  
16              
17             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             {
26 0     0 1   my $c = shift;
27 0   0       my $class = ref($c) || $c;
28 0           my $self = {};
29              
30 0           bless( $self, $class );
31 0 0         $self->_init(@_) or return undef;
32 0           return $self;
33             }
34              
35             sub _init
36             {
37 0     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 0         if ( ref($arg) eq 'HASH' ) { # hash ref?
    0          
46 0           foreach my $k ( keys %$arg ) {
47 0 0         if ( $self->can($k) ) {
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 (@_) {
55 0           my $k = shift;
56 0           my $v = shift;
57 0 0         if ( $self->can($k) ) {
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             {
73 0     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 0         if ( defined $arg ) {
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             {
94 0     0 1   my ( $self, $message ) = @_;
95 0           my $out = '';
96              
97 0           my @c = caller(1);
98 0 0         if ( substr( $c[3], 0, 5 ) eq 'main:' ) { @c = caller(2) }
  0            
99 0           my ( $package, $filename, $line, $subroutine, $hashargs, $wantarray, $evaltext, $is_require, $hints, $bitmask ) = @c;
100              
101 0 0         $message = $self unless ref($self);
102              
103 0           $out .= "$$: ";
104 0 0         $out .= "$subroutine: " if $subroutine;
105 0 0         $out .= "$message\n" if $message;
106 0           STDERR->print($out);
107             }
108              
109             # set the error string and return FAILURE
110             sub _error
111             {
112 0     0     my $self = shift;
113 0 0         $self->{error} .= "\n" if $self->{error};
114 0 0         $self->{error} = '' unless $self->{error};
115 0   0       $self->{error} .= "$self->{me}: " . ( shift || 'unknown error' );
116 0           return FAILURE;
117             }
118              
119             # get and clear error string
120             sub error
121             {
122 0     0 1   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             {
131 0     0 1   my ( $self, $o ) = @_;
132 0           my $sn = 'check_error';
133 0 0         if ($o) {
134 0           my $e = $o->error;
135 0 0         return $e ? $self->_error($e) : SUCCESS;
136             } else {
137 0           return $self->_error("$sn: no or unblessed object");
138             }
139             }
140              
141             1;
142              
143             __END__