Kite/Base.pm

filter: Statementstmt 89.8% 53 / 59 Branchbran 79.1% 19 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 88.0% 88 / 100 CC 15 SCAR 15.3
CC
15
Cov
87%
_dump
12.0
init
5.0
new
4.0
CRAP 4.6
« Kite/XML/Node.pm ↑ Summary Kite.pm »
1 #============================================================= -*-perl-*-
2 #
3 # Kite::Base
4 #
5 # DESCRIPTION
6 # Base class module implementing common functionality.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@kfs.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 # VERSION
18 # $Id: Base.pm,v 1.3 2000/10/17 11:58:16 abw Exp $
19 #
20 #========================================================================
21
22 package Kite::Base;
23  
24 6 require 5.004;
25  
26 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use vars qw( $VERSION $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 6 $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
30  
31  
32 #------------------------------------------------------------------------
33 # new(\%params)
34 #
35 # General purpose constructor method which expects a hash reference of
36 # configuration parameters, or a list of name => value pairs which are
37 # folded into a hash. Blesses a hash into an object and calls its
38 # _init() method, passing the parameter hash reference. Returns a new
39 # object derived from Kite::Base, or undef on error.
40 #------------------------------------------------------------------------
41  
42 20 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
43 20 my $class = shift;
44 20 ▶ my $params = (@_ && UNIVERSAL::isa($_[0], 'HASH')) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && UNIVERSAL::isa($_[0], 'HASH') ? : 4 16 20
Condition100%3/3
@_ and UNIVERSAL::isa($_[0], "HASH")
!ll&&!rl&&r
2144
MC/DC100%2/2
@_ and UNIVERSAL::isa($_[0], "HASH")
@_ UNIVERSAL::isa($_[0], "HASH")
Truth table100%3/3
A and B
A= @_B= UNIVERSAL::isa($_[0], "HASH")
ABresult
0X0
100
111
45 20 my $self = bless {
46 _ERROR => '',
47 }, $class;
48 20 ▶ return $self->init($params) ? $self : $class->error($self->error);
Branch: 1 branch
BranchTrueFalseTotal
$self->init($params) ? : 15 5 20
49 }
50  
51  
52 #------------------------------------------------------------------------
53 # init()
54 #
55 # Initialisation method called by the new() constructor, passing a
56 # reference to a hash array containing any configuration items specified
57 # as constructor arguments. Should return $self on success or undef on
58 # error, via a call to the error() method to set the error message.
59 #------------------------------------------------------------------------
60  
61 2 ▶sub init {
Subroutine init: called CC=5 SCAR=16.1
Pod documented
62 2 my ($self, $config) = @_;
63 2 my $class = ref $self;
64 2 my $params;
65  
66 # get a reference to the $PARAMS hash in the derived class package
67 {
68 6 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 $params = ${"$class\::PARAMS"};
70 }
71  
72 2 ▶ if (defined $params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 2 0 2
73 # initialise the $self object from the $config hash passed, using
74 # the $params hash to define acceptable parameters and defaults
75  
76 # map all config parameters to upper case
77 2 @$config{ map { uc $_ } keys %$config } = values %$config;
78  
79 # read parameters into $self from $config, using defaults if undefined
80 2 foreach my $key (keys %$params) {
81 6 ▶ if ($key =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^_/) { } 2 4 6
82 # just set default for private keys with leading _UNDERSCORE
83 2 $self->{ $key } = $params->{ $key };
84 }
85 else {
86 # otherwise use config value, if defined, or default
87 $self->{ $key } = defined $config->{ $key }
88 4 ▶ ? $config->{ $key } : $params->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{$key} ? : 3 1 4
89 }
90 }
91 }
92
93 2 return $self;
94 }
95  
96  
97 #------------------------------------------------------------------------
98 # error()
99 # error($msg, ...)
100 #
101 # May be called as a class or object method to set or retrieve the
102 # package variable $ERROR (class method) or internal member
103 # $self->{ ERROR } (object method). The presence of parameters indicates
104 # that the error value should be set. Undef is then returned. In the
105 # abscence of parameters, the current error value is returned.
106 #------------------------------------------------------------------------
107  
108 24 ▶sub error {
Subroutine error: called CC=3 SCAR=11.0
Pod documented
109 24 my $self = shift;
110 24 my $errvar;
111  
112 {
113 6 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
114 24 ▶ $errvar = ref $self ? \$self->{ _ERROR } : \${"$self\::ERROR"};
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 14 10 24
115 }
116 24 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 13 11 24
117 13 $$errvar = join('', @_);
118 13 return undef;
119 }
120 else {
121 11 return $$errvar;
122 }
123 }
124  
125  
126 #------------------------------------------------------------------------
127 # AUTOLOAD
128 #
129 # Autoload method.
130 #------------------------------------------------------------------------
131  
132 9 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
133 9 my $self = shift;
134 9 my $method = $AUTOLOAD;
135
136 9 $method =~ s/.*:://;
137 9 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 5 4 9
138
139 4 ▶ if ($method =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^_/) 1 3 4
140 1 my ($pkg, $file, $line) = caller();
141 1 die "attempt to access private member $method at $file line $line\n";
142 }
143  
144 3 $method = uc $method;
145 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 2 3
146 1 return ($self->{ $method } = shift);
147 }
148 else {
149 2 return $self->{ $method };
150 }
151 }
152  
153  
154 #------------------------------------------------------------------------
155 # _dump()
156 #
157 # Debug method to return a formatted string containing the object data.
158 #------------------------------------------------------------------------
159  
160 0 ▶sub _dump {
Subroutine _dump: not called CC=3 SCAR=24.8
161 0 my $self = shift;
162 0 my $text = "$self:\n";
163 0 ▶ while (my ($key, $value) = each %$self) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 0 0 0
164 0 ▶ $value = '<undef>' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
165 0 $text .= sprintf(" %-12s => $value\n", $key);
166 }
167 0 return $text;
168 }
169  
170  
171 6 1;
172  
173 __END__
« Kite/XML/Node.pm ↑ Summary Kite.pm »