in reply to mod_perl and objects

and form reading the docs, if methods are prototyped properly ($$), then they're invoked as object methods.
Nope! Prototypes and method-calling are orthogonal. The caller can call any sub as a method, and if a sub is called as a method, the prototype is ignored. You should probably get rid of the prototypes unless you have another reason.

I recommend giving perlobj a good read to understand method calls a bit better. The following two calls are equivalent:

My::Greeting->handler($r); ## class method My::Greeting::handler('My::Greeting', $r); ## equivalent
So when your handler sub is called as a method, and puts its first argument into $class, it contains the name of the class (a simple scalar). This makes things a little simpler for doing inheritance and subclassing. And that's why Data::Dumper is printing the name of the class -- perhaps you wanted to Dumper($r) instead? Or call handler as an object method ($obj->handler(...)) instead of a class method?

blokhead

Replies are listed 'Best First'.
Re: Re: mod_perl and objects
by grantm (Parson) on Jul 25, 2003 at 09:14 UTC

    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.

      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) = @_; ...
      i'll play around with it. i thought mod_perl 2.0 was still beta... but i see a guide to starting w/ mod_perl 2 on the modperl site

        Yes, mod_perl 2.0 is still in beta. There will be some work required to port from mod_perl 1.0 to mod_perl 2.0 so switching to a syntax that works in both may save pain later.

        You mentioned on the CB that the 'handler : method' syntax didn't work for you. It works for me on the stock standard Perl 5.6.1 and mod_perl 1.3.26 in Debian stable. It definitely won't work with Perl before 5.6 and may not work with earlier versions of mod_perl. I'm interested in what configuration doesn't work for you since I plan on releasing a module to CPAN that uses that syntax.

        Update: Sorry, 1.3.26 is obviously my Apache version number. I'm actually running mod_perl version 1.27

Re: Re: mod_perl and objects
by geektron (Curate) on Jul 25, 2003 at 08:38 UTC
    You should probably get rid of the prototypes unless you have another reason.

    i have no issue getting rid of them. as i said in the other reply, i thought there was some magick with constructors that happened with mod_perl because of the prototypes. i didn't think they really did much ..., and i've never bothered with them before this.

    when i did Dumper( $r ); i got the Request object -- not what i wanted. as previously noted, i thought a My::Greeting object was auto-magickally created.