CPAN is full of handlers, but what does a handler do that a subroutine or package method won't do?
There's really no answer to that question, because a "handler" can be a subroutine, or a function, or an anonymous code block -- it all depends on the context, and the specifics of hte system you're dealing with.
Generally speaking, when I see docs talking about "handlers" or "registering a handler" i think of it in terms of an "event handler" or a "callback" ... it's a way for an API to ask me to pass it either a coderef or the name of a method/subroutine/function that *I* have defined, and I want it to call whenever a given condition is met.
The simplest perl example is a signal handler, or warning handler in...
$SIG{__WARN__} = sub { print "there was a warning, my handler dealt with it", $_[0]; };
I am telling perl that anytime it encounters a warning, it should let my (anonymous) sub deal with it.
When should you use a handler?
When an API you are using requires you to specify one (or when you are writting an API in which it would be handy if your clients gave you one).
Are handler's difficult to write?
That depends. $SIG{__WAARN__} handlers are easy to write ... mod_perl apache handlers are a little tricker. it all depends on what the API is, what args your handler should expect to deal with, what your handler is expected to do with those args, and what return values the api is expecting to get from your handler.
In reply to Re: What is a handler?
by hossman
in thread What is a handler?
by McDuff
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |