modperl-modperl_cookbook_gotcha_for_newbies_required_framework_for_handlers

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: <modperl@perl.apache.org>
From: "Jeff" <jaa.perl@aquabolt.com>
Subject: mod_perl developers cookbook... a kitchen hand
Date: Sat, 23 Mar 2002 17:30:18 -0000

Just Curious of Hither Green writes: 

I feel like a right tit for asking this...

I already have mod_perl et al running, including my persistent DB
connections etc etc, but following gourmet cookery advice on this list
induced me to buy a copy of the mod_perl Developers Cookbook... and yes,
my nails were rather short after the week it took my Amazon to deliver
said arcane tome unto my abode.

So, I am working my way through, and get to page 83 which has a little
spellette:

sub handler {
  my $r = shift;
  print STDERR $r->as_string();
  return OK;
}

looks easy peasy - but

1) OK ->  Bareword "OK" not allowed while "strict subs" in use
   well, that's easy to fix - I must be missing a 'use' [which one??]
   I assume OK is 1 - ie TRUE

2) error log: Subroutine handler redefined at xxx line 1

This is interesting - probably because PerlHandler Apache::Registry so
kitchen whizzes, tell me please, exactly what knead I put in my
httpd.conf instead of Apache::Registry?

Thanks a munch!

Jeff


===

To: Jeff <jaa.perl@aquabolt.com>
From: Ade Olonoh <olonoh@yahoo.com>
Subject: Re: mod_perl developers cookbook... a kitchen hand
Date: 23 Mar 2002 12:49:57 -0500

 1) OK ->  Bareword "OK" not allowed while "strict subs" in use
>    well, that's easy to fix - I must be missing a 'use' [which one??]
>    I assume OK is 1 - ie TRUE

"OK" is a constant for the HTTP return code 200.  Add:

	use Apache::Constants ':common';

to the top of your prog. and it should work.


> 2) error log: Subroutine handler redefined at xxx line 1

Are you using Apache::StatINC?


===
To: Jeff <jaa.perl@aquabolt.com>
From: Geoffrey Young <geoff@modperlcookbook.org>
Subject: Re: mod_perl developers cookbook... a kitchen hand
Date: Sat, 23 Mar 2002 12:48:59 -0500

Jeff wrote:

 > Just Curious of Hither Green writes:
 >
 > I feel like a right tit for asking this...
 >
 > I already have mod_perl et al running, including my persistent DB connections
 >  etc etc, but following gourmet cookery advice on this list induced me to
 > buy a copy of the mod_perl Developers Cookbook... and yes, my nails were
 > rather short after the week it took my Amazon to deliver said arcane tome
 > unto my abode.


I hope you enjoy it and that you didn't bite to the quick...


 >
 > So, I am working my way through, and get to page 83 which has a little spellette:
 >

 >
 > sub handler { my $r = shift; print STDERR $r->as_string(); return OK; }
 >
 > looks easy peasy - but
 >
 > 1) OK ->  Bareword "OK" not allowed while "strict subs" in use well, that's
 > easy to fix - I must be missing a 'use' [which one??] I assume OK is 1 - ie
 >  TRUE


OK is actually 0, but is better used as

use Apache::Constants qw(OK);

as to avoid any real value behind the OK constant.  see recipe 3.12 for more 
details about this notation.


 >
 > 2) error log: Subroutine handler redefined at xxx line 1


well, you have to put handler() in a package first... depending on which package
you put it in, it might redefine something that already exists.


you're coming up against something I struggled with throughout the book.  lots 
of our examples use handler snippets instead of the complete handler.  this is 
because I thought doing this

package Cookbook::Foo;

use Apache::Constants qw(:common);
use strict;

sub handler {
  ...
}

1;

wasted lots of space - in this particular instance the other cruft is longer 
than the code we were illustrating :)

so I basically took the point of view that for shortish stuff enforcing the 
handler() meme was good enough, and the package stuff could be reinforced 
elsewhere.  and there's always the eagle book in addition to our stuff.

I hope that as you read through the rest of the book things start solidifying...

HTH

===

To: Ade Olonoh <olonoh@yahoo.com>
From: Geoffrey Young <geoff@modperlcookbook.org>
Subject: Re: mod_perl developers cookbook... a kitchen hand
Date: Sat, 23 Mar 2002 12:49:25 -0500

> 
> "OK" is a constant for the HTTP return code 200.  


close.  OK is 0, which is different from HTTP_OK which is 200.

===

To: "Jeff" <jaa.perl@aquabolt.com>
From: Per Einar Ellefsen <per.einar@skynet.be>
Subject: Re: mod_perl developers cookbook... a kitchen hand
Date: Sat, 23 Mar 2002 18:51:21 +0100

At 17:30 23.03.2002 +0000, Jeff wrote:
>Just Curious of Hither Green writes:
>
>So, I am working my way through, and get to page 83 which has a little
>spellette:
>
>sub handler {
>   my $r = shift;
>   print STDERR $r->as_string();
>   return OK;
>}
>
>looks easy peasy - but
>
>1) OK ->  Bareword "OK" not allowed while "strict subs" in use
>    well, that's easy to fix - I must be missing a 'use' [which one??]
>    I assume OK is 1 - ie TRUE
>
>2) error log: Subroutine handler redefined at xxx line 1
>
>This is interesting - probably because PerlHandler Apache::Registry so
>kitchen whizzes, tell me please, exactly what knead I put in my
>httpd.conf instead of Apache::Registry?

You must have taken this subroutine out of context. There are a certain 
number of things which must appear for an Apache handler to work:

package Apache::Whatever;

You need to have that line to uniquely identify your module. If you use the 
name Apache::Whatever, your handler must be named Whatever.pm and be placed 
in your @INC search path under Apache/
This is probably the reason for your number 2 problem: you must have used 
the same package name twice. Or not used a package name at all.

use Apache::Constants;

This imports the constants like OK, DECLINED, etc... You can also specify 
them explicitly:
use Apache::Constants qw(OK DECLINED);

I hope this helps you out.

===
To: Per Einar Ellefsen <per.einar@skynet.be>
From: Issac Goldstand <margol@beamartyr.net>
Subject: Re: mod_perl developers cookbook... a kitchen hand
Date: Mon, 25 Mar 2002 02:23:12 +0200

>
> You must have taken this subroutine out of context. There are a 
> certain number of things which must appear for an Apache handler to work:
>
> package Apache::Whatever;
>
> You need to have that line to uniquely identify your module. If you 
> use the name Apache::Whatever, your handler must be named Whatever.pm 
> and be placed in your @INC search path under Apache/
> This is probably the reason for your number 2 problem: you must have 
> used the same package name twice. Or not used a package name at all.


Actually, this isn't true.. You don't *have* to use the Apache 
namespace...  You can just as easily call it Foo::Bar if you'd like... 
 I don't mean to come across as being an extremist on this issue, but we 
were all newbies once, and I know that as a newbie, *I* would take 
anything written here as the "authorative" answer, so I didn't want 
anyone here to be misled...

===

To: Issac Goldstand <margol@beamartyr.net>
From: Per Einar Ellefsen <per.einar@skynet.be>
Subject: Re: mod_perl developers cookbook... a kitchen hand
Date: Mon, 25 Mar 2002 07:24:42 +0100

At 01:23 25.03.2002, Issac Goldstand wrote:

>>You must have taken this subroutine out of context. There are a certain 
>>number of things which must appear for an Apache handler to work:
>>
>>package Apache::Whatever;
>>
>>You need to have that line to uniquely identify your module. If you use 
>>the name Apache::Whatever, your handler must be named Whatever.pm and be 
>>placed in your @INC search path under Apache/
>>This is probably the reason for your number 2 problem: you must have used 
>>the same package name twice. Or not used a package name at all.
>
>
>Actually, this isn't true.. You don't *have* to use the Apache 
>namespace...  You can just as easily call it Foo::Bar if you'd like... I 
>don't mean to come across as being an extremist on this issue, but we were 
>all newbies once, and I know that as a newbie, *I* would take anything 
>written here as the "authorative" answer, so I didn't want anyone here to 
>be misled...

Yes, sorry, I didn't want to make it appear like you had to use the 
Apache:: namespace. In the Cookbook for example they use the Cookbook:: 
namespace, and you could just as well use Jeff::.


===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu