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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.