McDuff has asked for the wisdom of the Perl Monks concerning the following question:

This is a really basic question. A simple / clear answer would help:

What is a handler?

CPAN is full of handlers, but what does a handler do that a subroutine or package method won't do?

When should you use a handler?

Are handler's difficult to write?

Is there a URL or other source about the "a,b,c's" of hanlders (perhaps with examples in PERL code)?

Tnx. McDuff

Title edit by tye

Replies are listed 'Best First'.
Re: Handlers
by jdtoronto (Prior) on Nov 17, 2003 at 14:12 UTC
    Although I have never seen a definition, the term handler is usually applied to something which is not of itself an autonomous function, but which fits into a function to provide a service.

    As an example, look at the Apache Web Server. The server software provides hooks where you can interpose code for things such as authentication, authorization, request handling and others. Each of these is known as a phase becuase one pphase starts and completes before another phase can commence. Within the phase there is a provision ofr external code to be used to perform a function in a different way to that which Apache provides itself. The handler code will receive a set of data or structures, process them, and returns a response. For example you might use several types of authorization methods on your web site. Some areas might be protected by Basic .htaccess controls, others might require validation against a database. So you have two handlers. One for the .htaccess method, if it fails Apache will keep checking with any handlers it has been told about until it finds on e that fits, say the database handler, when it returns a success the process is completed and the server moves on to the next phase.

    The Apache handler based system is well documented on the Apache web site and in the various books on the server.

    Hope that helps
    jdtoronto

Re: What is a handler?
by hossman (Prior) on Nov 17, 2003 at 23:14 UTC
    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.

Re: What is a handler?
by jeffa (Bishop) on Nov 17, 2003 at 19:41 UTC
    Here is a super trivial example. Even though my objects are derrived from a base class, they don't have to be ... the only real requirement is that they have some predetermined method, say handle(), that can be invoked:
    package main; print $_->handle for (Bar->new, Baz->new); package Foo; sub new { return bless {}, shift } package Bar; use base 'Foo'; sub handle { return "Bar's handler was called\n" } package Baz; use base 'Foo'; sub handle { return "Baz's handler was called\n" }
    This is also called polymorphism, by the way.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: What is a handler?
by Anonymous Monk on Nov 17, 2003 at 21:55 UTC
    1. What is a handler?
      handler n 1: one who trains or exhibits animals [syn: {animal trainer}] 2: an agent who handles something or someone; "the senator's campaign handlers" 3: (sports) someone in charge of training an athlete or a team [syn: {coach}, {manager}]
    2. CPAN is full of handlers, but what does a handler do that a subroutine or package method won't do?
      Everything (have you looked at any of those "handlers"?)
    3. When should you use a handler?
      Whenever you need to handle something.
    4. Are handler's difficult to write?
      Are programs difficult to write? Is code difficult to write?
    5. Is there a URL or other source about the "a,b,c's" of hanlders (perhaps with examples in PERL code)?
      Handlers don't have a's, b's or c's. Handler is a generic term (see definition 2 above), it's not programming theory.