In general, you're correct: Perl prototypes have nothing to do with subroutines being called as methods. However the original poster was asking about mod_perl handlers and is quite correct in interpreting the docs to infer a relationship between prototypes and handlers being called as methods.

When you write a mod_perl handler, you create a module (say YourModule) and in it you create a subroutine called handler. At the appropriate point in the request chain, mod_perl will create a request object $r and call your handler like this:

YourModule::handler($r)

However a piece of magic comes into play if you defined your handler sub with a prototype like this:

sub handler ($$) { my($class, $r) = @_; ...

In this case, mod_perl will invoke your handler as a class method like this:

YourModule->handler($r)

The advantage of this is that someone else can subclass YourModule and override one or two methods, but they can inherit the handler method from your module.

Actually I'd recommend against using the prototyped handler syntax in new code as it's not compatible with mod_perl 2.0 (which can pass more arguments to a handler). The new way to do it is to use the 'method' attribute on your subroutine definition:

sub handler : method { my($class, $r) = @_; ...

This will work in mod_perl 1.0 or 2.0 as long as you're running Perl 5.6 or better.

The original poster's problem was expecting the handler to be invoked as an object method. If you need an instance of your class then you need to call your constructor to create one yourself in the handler.


In reply to Re: Re: mod_perl and objects by grantm
in thread mod_perl and objects by geektron

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.