Template::Base - Base class module implementing common functionality
package My::Module; use base qw( Template::Base );
sub _init { my ($self, $config) = @_; $self->{ doodah } = $config->{ doodah } || return $self->error("No 'doodah' specified"); return $self; }
package main;
my $object = My::Module->new({ doodah => 'foobar' }) || die My::Module->error();
my $module = My::Module->new({ ... }) || die My::Module->error(), "\n";
my $module = My::Module->new({ ... }) || die "constructor error: $My::Module::ERROR\n";
my $module = My::Module->new({ ... }) || die My::Module->error(), "\n";
$module->do_something() || die $module->error(), "\n";
When called with parameters (multiple params are concatenated), this method will set the relevant variable and return undef. This is most often used within object methods to report errors to the caller.
package My::Module;
sub foobar { my $self = shift;
# some other code...
return $self->error('some kind of error...') if $some_condition; }
package My::Module;
sub foobar { my $self = shift;
$self->debug('called foobar()');
# some other code... }
When the foobar() method is called, the following message is sent to STDERR:
[My::Module] called foobar()
Objects can set an internal DEBUG value which the debug() method will examine. If this value sets the relevant bits to indicate DEBUG_CALLER then the file and line number of the caller will be appened to the message.
use Template::Constants qw( :debug );
my $module = My::Module->new({ DEBUG => DEBUG_SERVICE | DEBUG_CONTEXT | DEBUG_CALLER, });
$module->foobar();
This generates an error message such as:
[My::Module] called foobar() at My/Module.pm line 6
<http://www.andywardley.com/|http://www.andywardley.com/>
Copyright (C) 1996-2004 Andy Wardley. All Rights Reserved. Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |