| 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
Condition100%3/3
@_ and UNIVERSAL::isa($_[0], "HASH")
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")
| |||||||||||||||||||||||||||||
| 45 | 20 | my $self = bless { | |||||||||||||||||||||||||||
| 46 | _ERROR => '', | ||||||||||||||||||||||||||||
| 47 | }, $class; | ||||||||||||||||||||||||||||
| 48 | 20 | ▶ | return $self->init($params) ? $self : $class->error($self->error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 24 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | ▶ | if ($method =~ /^_/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | $value = '<undef>' unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | $text .= sprintf(" %-12s => $value\n", $key); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 0 | return $text; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 6 | 1; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||