| 1 | #============================================================= -*-perl-*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Template::Base | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Base class module implementing common functionality for various other | ||||||||||||||||||||||||||||
| 7 | # Template Toolkit modules. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # AUTHOR | ||||||||||||||||||||||||||||
| 10 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # COPYRIGHT | ||||||||||||||||||||||||||||
| 13 | # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 16 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | #======================================================================== | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Template::Base; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 117 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 117 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 117 | ▶ | use Template::Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 117 | our $VERSION = '3.106'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 30 | # new(\%params) | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | # General purpose constructor method which expects a hash reference of | ||||||||||||||||||||||||||||
| 33 | # configuration parameters, or a list of name => value pairs which are | ||||||||||||||||||||||||||||
| 34 | # folded into a hash. Blesses a hash into an object and calls its | ||||||||||||||||||||||||||||
| 35 | # _init() method, passing the parameter hash reference. Returns a new | ||||||||||||||||||||||||||||
| 36 | # object derived from Template::Base, or undef on error. | ||||||||||||||||||||||||||||
| 37 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1901 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 1901 | my $class = shift; | |||||||||||||||||||||||||||
| 41 | 1901 | my ($argnames, @args, $arg, $cfg); | |||||||||||||||||||||||||||
| 42 | # $class->error(''); # always clear package $ERROR var? | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 117 | ▶ | { no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 117 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1901 | ▶ | $argnames = \@{"$class\::BASEARGS"} || [ ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
\@{"${class}::BASEARGS";} || []
MC/DC0%0/1
\@{"${class}::BASEARGS";} || []
\@{"${class}::BASEARGS";}
Truth table50%1/2
A
A= \@{"${class}::BASEARGS";}
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # shift off all mandatory args, returning error if undefined or null | ||||||||||||||||||||||||||||
| 50 | 1901 | foreach $arg (@$argnames) { | |||||||||||||||||||||||||||
| 51 | 66 | ▶ | return $class->error("no $arg specified") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | unless ($cfg = shift); | ||||||||||||||||||||||||||||
| 53 | 66 | push(@args, $cfg); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # fold all remaining args into a hash, or use provided hash ref | ||||||||||||||||||||||||||||
| 57 | 1901 | ▶ | $cfg = defined $_[0] && ref($_[0]) eq 'HASH' ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[0] and ref $_[0] eq "HASH"
MC/DC100%2/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0]
ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | my $self = bless { | ||||||||||||||||||||||||||||
| 60 | 1901 | (map { ($_ => shift @args) } @$argnames), | |||||||||||||||||||||||||||
| 61 | _ERROR => '', | ||||||||||||||||||||||||||||
| 62 | DEBUG => 0, | ||||||||||||||||||||||||||||
| 63 | }, $class; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1901 | ▶ | return $self->_init($cfg) ? $self : $class->error($self->error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 70 | # error() | ||||||||||||||||||||||||||||
| 71 | # error($msg, ...) | ||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||
| 73 | # May be called as a class or object method to set or retrieve the | ||||||||||||||||||||||||||||
| 74 | # package variable $ERROR (class method) or internal member | ||||||||||||||||||||||||||||
| 75 | # $self->{ _ERROR } (object method). The presence of parameters indicates | ||||||||||||||||||||||||||||
| 76 | # that the error value should be set. Undef is then returned. In the | ||||||||||||||||||||||||||||
| 77 | # absence of parameters, the current error value is returned. | ||||||||||||||||||||||||||||
| 78 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 98 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 98 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 98 | my $errvar; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 117 | ▶ | no strict qw( refs ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 98 | ▶ | $errvar = ref $self ? \$self->{ _ERROR } : \${"$self\::ERROR"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 98 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 50 | ▶ | $$errvar = ref($_[0]) ? shift : join('', @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 50 | return undef; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 48 | return $$errvar; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 99 | # _init() | ||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||
| 101 | # Initialisation method called by the new() constructor and passing a | ||||||||||||||||||||||||||||
| 102 | # reference to a hash array containing any configuration items specified | ||||||||||||||||||||||||||||
| 103 | # as constructor arguments. Should return $self on success or undef on | ||||||||||||||||||||||||||||
| 104 | # error, via a call to the error() method to set the error message. | ||||||||||||||||||||||||||||
| 105 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 4 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 109 | 4 | return $self; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 0 | my $msg = join('', @_); | |||||||||||||||||||||||||||
| 116 | 0 | my ($pkg, $file, $line) = caller(); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | unless (substr($msg,-1) eq "\n") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $msg .= ($self->{ DEBUG } & Template::Constants::DEBUG_CALLER) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | ? " at $file line $line\n" | ||||||||||||||||||||||||||||
| 121 | : "\n"; | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | print STDERR "[$pkg] $msg"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 129 | # module_version() | ||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||
| 131 | # Returns the current version number. | ||||||||||||||||||||||||||||
| 132 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | sub module_version { | ||||||||||||||||||||||||||
|
Subroutine
module_version: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 2 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 137 | 117 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 2 | return ${"${class}::VERSION"}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3669 | ▶ | sub DESTROY { 1 } # noop | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 117 | 1; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||