line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================================= -*-perl-*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Kite::Base |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# Base class module implementing common functionality. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# AUTHOR |
9
|
|
|
|
|
|
|
# Andy Wardley |
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
|
|
|
|
|
|
|
require 5.004; |
25
|
|
|
|
|
|
|
|
26
|
6
|
|
|
6
|
|
1768
|
use strict; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
193
|
|
27
|
6
|
|
|
6
|
|
27
|
use vars qw( $VERSION $AUTOLOAD ); |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
2325
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
sub new { |
43
|
20
|
|
|
20
|
1
|
219
|
my $class = shift; |
44
|
20
|
100
|
100
|
|
|
190
|
my $params = (@_ && UNIVERSAL::isa($_[0], 'HASH')) ? shift : { @_ }; |
45
|
20
|
|
|
|
|
83
|
my $self = bless { |
46
|
|
|
|
|
|
|
_ERROR => '', |
47
|
|
|
|
|
|
|
}, $class; |
48
|
20
|
100
|
|
|
|
95
|
return $self->init($params) ? $self : $class->error($self->error); |
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
|
|
|
|
|
|
|
sub init { |
62
|
2
|
|
|
2
|
1
|
11
|
my ($self, $config) = @_; |
63
|
2
|
|
|
|
|
5
|
my $class = ref $self; |
64
|
2
|
|
|
|
|
3
|
my $params; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# get a reference to the $PARAMS hash in the derived class package |
67
|
|
|
|
|
|
|
{ |
68
|
6
|
|
|
6
|
|
30
|
no strict qw( refs ); |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
1306
|
|
|
2
|
|
|
|
|
3
|
|
69
|
2
|
|
|
|
|
3
|
$params = ${"$class\::PARAMS"}; |
|
2
|
|
|
|
|
8
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
2
|
50
|
|
|
|
5
|
if (defined $params) { |
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
|
|
|
|
|
8
|
@$config{ map { uc $_ } keys %$config } = values %$config; |
|
3
|
|
|
|
|
11
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# read parameters into $self from $config, using defaults if undefined |
80
|
2
|
|
|
|
|
13
|
foreach my $key (keys %$params) { |
81
|
6
|
100
|
|
|
|
17
|
if ($key =~ /^_/) { |
82
|
|
|
|
|
|
|
# just set default for private keys with leading _UNDERSCORE |
83
|
2
|
|
|
|
|
11
|
$self->{ $key } = $params->{ $key }; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
|
|
|
|
|
|
# otherwise use config value, if defined, or default |
87
|
4
|
100
|
|
|
|
19
|
$self->{ $key } = defined $config->{ $key } |
88
|
|
|
|
|
|
|
? $config->{ $key } : $params->{ $key }; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
2
|
|
|
|
|
10
|
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
|
|
|
|
|
|
|
sub error { |
109
|
24
|
|
|
24
|
1
|
67
|
my $self = shift; |
110
|
24
|
|
|
|
|
23
|
my $errvar; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
{ |
113
|
6
|
|
|
6
|
|
29
|
no strict qw( refs ); |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
2399
|
|
|
24
|
|
|
|
|
25
|
|
114
|
24
|
100
|
|
|
|
67
|
$errvar = ref $self ? \$self->{ _ERROR } : \${"$self\::ERROR"}; |
|
10
|
|
|
|
|
42
|
|
115
|
|
|
|
|
|
|
} |
116
|
24
|
100
|
|
|
|
94
|
if (@_) { |
117
|
13
|
|
|
|
|
34
|
$$errvar = join('', @_); |
118
|
13
|
|
|
|
|
114
|
return undef; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
11
|
|
|
|
|
49
|
return $$errvar; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
127
|
|
|
|
|
|
|
# AUTOLOAD |
128
|
|
|
|
|
|
|
# |
129
|
|
|
|
|
|
|
# Autoload method. |
130
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub AUTOLOAD { |
133
|
4
|
|
|
4
|
|
64
|
my $self = shift; |
134
|
4
|
|
|
|
|
6
|
my $method = $AUTOLOAD; |
135
|
|
|
|
|
|
|
|
136
|
4
|
|
|
|
|
19
|
$method =~ s/.*:://; |
137
|
4
|
50
|
|
|
|
10
|
return if $method eq 'DESTROY'; |
138
|
|
|
|
|
|
|
|
139
|
4
|
100
|
|
|
|
12
|
if ($method =~ /^_/) { |
140
|
1
|
|
|
|
|
4
|
my ($pkg, $file, $line) = caller(); |
141
|
1
|
|
|
|
|
11
|
die "attempt to access private member $method at $file line $line\n"; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
3
|
|
|
|
|
17
|
$method = uc $method; |
145
|
3
|
100
|
|
|
|
9
|
if (@_) { |
146
|
1
|
|
|
|
|
5
|
return ($self->{ $method } = shift); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
else { |
149
|
2
|
|
|
|
|
10
|
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
|
|
|
|
|
|
|
sub _dump { |
161
|
0
|
|
|
0
|
|
|
my $self = shift; |
162
|
0
|
|
|
|
|
|
my $text = "$self:\n"; |
163
|
0
|
|
|
|
|
|
while (my ($key, $value) = each %$self) { |
164
|
0
|
0
|
|
|
|
|
$value = '' unless defined $value; |
165
|
0
|
|
|
|
|
|
$text .= sprintf(" %-12s => $value\n", $key); |
166
|
|
|
|
|
|
|
} |
167
|
0
|
|
|
|
|
|
return $text; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
1; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
__END__ |