<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">Name
---------

Errors::Errors - Module for error/die/exit/abort proceeding.


Synopsis
-------------

use Errors::Errors;
$obj = Errors::Errors-&gt;new();

$obj-&gt;attach('some'); # Attach sub object for error of type 'some'

$obj-&gt;install('onTerm',\&amp;custom);
$obj-&gt;install('onError',\&amp;anysub,'some');
$obj-&gt;install('onExit',\&amp;leave);
$obj-&gt;exit();
sub custom {
my $obj = shift; # 'Errors' object
my $err = shift; # Error number/message (for TERM it has value 'term')
my $name = shift; # 'name' of error (for TERM it has empty value)
# ...blah...blah...
}
sub leave {
my $obj = shift; # 'Errors' object
my $err = shift; # Last error number/message
my $name = shift; # 'name' of error
my $how = shift; # can be: 'exit','die' or 'destroy'
# ...blah...blah...
}
sub anysub {
my $obj = shift; # 'Errors' object
my $err = shift; # Error number/message
my $name = shift; # 'name' of error
if($name =~ m/some/si)
{
$obj-&gt;print ("Error in some!");
}
else
{
$obj-&gt;print ("Error in ... I don't know :-)!!!");
}
}


Install
---------

As any other 'CPAN' module you just have to do follow simple steps to complete 
installation:

tar -zxvf Errors-1.01.tar.gz
cd Errors-1.01
perl Makefile.PL
make
make test
make install
make clean

After successful installation you can get explore all these pretty stuff :-)


Getting started
---------------------

First question is: Why I have to use Errors module in my Perl program?
In large projects you have to use many libraries, modules and of course you have 
to catch all errors in, also in some cases you need to make some finalize 
procedures at the end of your scripts. To do that you may need to write subs for 
different errors, to write code for interrupts/events/signals but what structure 
you use? None? Huh! You just write code and grubby you program! It's a disgrace. 

This module offer to you and your scripts centralize errors handling and 
proceeding.


How it's works?
----------------------

First you must to create one object for your program:

use Errors::Errors;
$obj = Errors::Errors-&gt;new();

This object will contain pointers to your subs that handle occurred errors.
This module can handle follow base errors/events:

exit(onExit), die(onExit), TERM/STOP/PIPE/QUIT(onTerm), ALRM(onTimeout), and 
custom errors(onError).

If you want to do something when your script ends, set your custom sub:

$obj-&gt;install('onExit',\&amp;leave);

where \&amp;leave is pointer to your sub 'leave'. When your script ends your sub 

program 'leave' will be executed before the end. That will be happened at the 
end of $obj scope i.e. in DESTROY section of module. Also you can provoke 
execution of 'leave' if you write follow line in program:

$obj-&gt;exit($code_error);
or
$obj-&gt;die($code_error);

If you want to handle ALRM signal write line like this:

$obj-&gt;install('onTimeout',\&amp;custom);
where 'custom' is your custom sub that handle ALRM signal.

To handle TERM/STOP/PIPE/QUIT signals use follow like:

$obj-&gt;install('onTerm',\&amp;custom);

where 'custom' is your custom sub that handle listed signals.

If you want to initialize your custom errors write code bellow:

$obj-&gt;install('onError',\&amp;anysub);

So when you call method bellow, somewhere in script, you will rise execution of 
'anysub'!

$obj-&gt;error($code_error);

Of course all your subs will receive additional parameters so you will be able 
to find reason for errors (and respective error codes). See SYSNOPSIS above.
In single script these methods are strong enough, but if you have a complex 
program with lot of libraries/modules you may want to have a single 'errors' 
object for all your libraries/modules. But how you can handle TERM signal for 
all yours different libraries??? The idea is quite simple, we will still 
continue to use our global (single) object, but we will create "sub objects" for 
all additional libraries/modules! 

To add new sub object do follow:

$obj-&gt;attach('some');

This will create sub object called 'some'. To install 'onTerm' signal for this 
sub object (SObject) do follow:

$obj-&gt;install('onTerm',\&amp;custom,'some');

Also to catch 'exit' for SObject call:

$obj-&gt;install('onExit,',\&amp;leave,'some');

To rise custom error for 'some' SObject, call:

$obj-&gt;error($code_error,'some');

To exit…:

$obj-&gt;exit($code_error,'some');
… and so on.

Note:
$obj-&gt;error($code_error,'some');
will rise error in 'some' sub object
$obj-&gt;error($code_error);

will rise error in 'main' object!!! (Think about that like parent object and 
respective children)!

Let imagine that we have a lot of children (SObjects) and one parent object. 
Also let for all these objects we have sub programs that catch exit event. And 
now let our program gone to the end of code, so program must quit and Perl must 
destroy and unload our program!
However Errors module have job to do, it must call all subs that are joined into 
'exit' chain!

See follow block:

foreach $obj call {
| SObject 1 | -&gt; onExit sub
| SObject 2 | -&gt; onExit sub
…
| SObject n | -&gt; onExit sub
| Main object | -&gt; onExit sub
}

All this will be happened with all other events/signals! So you have to check 
reason and decide whether you must do anything into respective sub! See simple 

example:

sub anysub {
my $obj = shift; # 'Errors' object
my $err = shift; # Error number/message
my $name = shift; # 'name' of error
if($name =~ m/some/si)
{
$obj-&gt;print ("Error in some!");
}
else
{
$obj-&gt;print ("Error in ... I don't know :-)!!!");
}
}

If name is 'some' we can do something, but when the error is somewhere else you 
may want to miss it! You have to decide!

To delete SObject call:

$obj-&gt;detach('some');
To uninstall event/signal:
$obj-&gt;uninstall('onError','some'); # for some SObject
or
$obj-&gt;uninstall('onError'); # for main object

At the end I would like to notice that this module was originally written for 
Web and more correct for WebTools Perl sub system (look at 
http://www.proscriptum.com/)

So to allow Errors object to let know, whether content type is sent use follow 
line:

$obj-&gt;content($state); # $state can be '1' - yes and '0' - is not sent yet!

If you want to send default content (HTTP) header use:

$obj-&gt;header();

Note: If $obj-&gt;content() is '1' then $obj-&gt;header() will return immediately 
without printing default header, but if you are not still sent header (or if you 
forgot to tell that to Error object via $obj-&gt;content(1) ) then $obj-&gt;header() 
will sent default header.

To overwrite default header function use statement bellow:

$obj-&gt;install('header',\&amp;your_header_sub);

All this (header and content functions) is needful because if this script is WEB 
based and error occurred you may want to print some text into browser, but if 
you forgot to print content, error will occurred (Internal Error). And vice 
versa: If content is already sent and you print it again then second content 
header will appear in browser :-(

For more information, look at Errors::Errors module.

Author
---------

Julian Lishev,

E-mail: julian@proscriptum.com
URL: http://www.proscriptum.com/</pre></body></html>