1Template::Base(3)     User Contributed Perl Documentation    Template::Base(3)
2
3
4

NAME

6       Template::Base - Base class module implementing common functionality
7

SYNOPSIS

9           package My::Module;
10           use base qw( Template::Base );
11
12           sub _init {
13               my ($self, $config) = @_;
14               $self->{ doodah } = $config->{ doodah }
15                   || return $self->error("No 'doodah' specified");
16               return $self;
17           }
18
19           package main;
20
21           my $object = My::Module->new({ doodah => 'foobar' })
22               || die My::Module->error();
23

DESCRIPTION

25       Base class module which implements a constructor and error reporting
26       functionality for various Template Toolkit modules.
27

PUBLIC METHODS

29   new(\%config)
30       Constructor method which accepts a reference to a hash array or a list
31       of "name => value" parameters which are folded into a hash.  The
32       "_init()" method is then called, passing the configuration hash and
33       should return true/false to indicate success or failure.  A new object
34       reference is returned, or undef on error.  Any error message raised can
35       be examined via the error() class method or directly via the $ERROR
36       package variable in the derived class.
37
38           my $module = My::Module->new({ ... })
39               || die My::Module->error(), "\n";
40
41           my $module = My::Module->new({ ... })
42               || die "constructor error: $My::Module::ERROR\n";
43
44   error($msg, ...)
45       May be called as an object method to get/set the internal "_ERROR"
46       member or as a class method to get/set the $ERROR variable in the
47       derived class's package.
48
49           my $module = My::Module->new({ ... })
50               || die My::Module->error(), "\n";
51
52           $module->do_something()
53               || die $module->error(), "\n";
54
55       When called with parameters (multiple params are concatenated), this
56       method will set the relevant variable and return undef.  This is most
57       often used within object methods to report errors to the caller.
58
59           package My::Module;
60
61           sub foobar {
62               my $self = shift;
63
64               # some other code...
65
66               return $self->error('some kind of error...')
67                   if $some_condition;
68           }
69
70   debug($msg, ...)
71       Generates a debugging message by concatenating all arguments passed
72       into a string and printing it to "STDERR".  A prefix is added to
73       indicate the module of the caller.
74
75           package My::Module;
76
77           sub foobar {
78               my $self = shift;
79
80               $self->debug('called foobar()');
81
82               # some other code...
83           }
84
85       When the "foobar()" method is called, the following message is sent to
86       "STDERR":
87
88           [My::Module] called foobar()
89
90       Objects can set an internal "DEBUG" value which the "debug()" method
91       will examine.  If this value sets the relevant bits to indicate
92       "DEBUG_CALLER" then the file and line number of the caller will be
93       append to the message.
94
95           use Template::Constants qw( :debug );
96
97           my $module = My::Module->new({
98               DEBUG => DEBUG_SERVICE | DEBUG_CONTEXT | DEBUG_CALLER,
99           });
100
101           $module->foobar();
102
103       This generates an error message such as:
104
105           [My::Module] called foobar() at My/Module.pm line 6
106
107   module_version()
108       Returns the version number for a module, as defined by the $VERSION
109       package variable.
110

AUTHOR

112       Andy Wardley <abw@wardley.org> <http://wardley.org/>
113
115       Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.
116
117       This module is free software; you can redistribute it and/or modify it
118       under the same terms as Perl itself.
119

SEE ALSO

121       Template
122
123
124
125perl v5.32.1                      2021-01-27                 Template::Base(3)
Impressum