Gossamer Forum
Home : General : Perl Programming :

mod_perl

Quote Reply
mod_perl
I've just got a script I've written to work with mod_perl and it is pretty rapid.

I was just looking for any pointers and things I need to watch out for when using mod_perl

I notice Links SQL etc use reset_env methods....are these essential?....I don't do any resetting at the moment and it seems to work ok but what if anything should I be resetting?

I have a DESTROY method in some of my modules to destroy database handles and objects etc...

Any tips would be great thanks.

Last edited by:

Paul: Jun 12, 2002, 3:07 AM
Quote Reply
Re: [Paul] mod_perl In reply to
I'm not an expert, but I find http://perl.apache.org/#docs quite useful, especially the mod_perl guide located at http://perl.apache.org/guide/

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Paul] mod_perl In reply to
I've found the mod_perl CGI FAQ to be a good starting point, too:

http://perl.apache.org/faq/mod_perl_cgi.html

But if I were you I'd go out and buy myself a decent book (or whatever else your favorite method lf learning is) first before you sit down and hack away. Writing for mod_perl requires understanding of some other things, like memory management and the Apache process model. With mod_perl it's quite easy to create some scripts with bad memory_leakage.

Saying this, I consider yourself to be a fairly competent programmer, and I know that you run most of your scripts under use script. And, in my opinion, only under mod_perl one really sees the beauty of using use strict. As the biggest problem is memory leakage of people not destroying their global variables. If you're running, and understand the concepts of use strict, then this shouldn't byte you in the ass, and you should really be fine with creating private variables that do not go walkies into your global package and never get destroyed.

Be very careful with your global variables, and treat them like the plague and you shouldn't have any problems.

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
Yeah I always use strict.

I think I have a fairly decent idea of what Im doing, I just wanted to be extra certain (Im probably just being paranoid in all truth).

Im destroying all objects using a DESTROY block and I've just added a reset method to reset my $WO::Class::error variables and a few other bits and bobs.

It seems to be running fine....if it wasn't, when I restart apache it would seriously barf but I get no warnings or errors.
Quote Reply
Re: [Paul] mod_perl In reply to
Data::Dumper could also be useful to you here in assesing your data strucutres. Apart from that, just keep an eye on your memory's usage, I guess. If it brings your server to it's knees you know that something is wrong :-)

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
Why would I want to assess my data structures?

Last edited by:

Paul: Jun 12, 2002, 5:33 AM
Quote Reply
Re: [Paul] mod_perl In reply to
it's a *very* handy way of seeing what your data looks like. I'd also recommend Data::BFDump for more in-depth analysis.

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
Yeah I know what it does Smile but I'm asking why I'd want to see what it looks like?
Quote Reply
Re: [Paul] mod_perl In reply to
Because things might not seem waht they look like. I've often thought to myself that I think I know what my data strucutres does but have been surprised that I'm wasting memory etc.

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
I tend to use ref to verify that the variable is what I think it is.
Quote Reply
Re: [Paul] mod_perl In reply to
What about large complex arrays? :-)

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
What about them?
Quote Reply
Re: [Paul] mod_perl In reply to
How can you check those?

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
Check them for what?
Quote Reply
Re: [Paul] mod_perl In reply to
Check their strucutre to see if they are structed the way you think that they are structured.

- wil
Quote Reply
Re: [Wil] mod_perl In reply to
Code:

my $array = [ [qw/I am ok/], { "I am" => 'not' } ];
if (ref $array eq 'ARRAY') {
for (@$array) {
ref $_ eq 'ARRAY' or die "I found: $_. This isn't an array reference so we don't have an arrayref of arrayrefs\n";
}
}
Quote Reply
Re: [Paul] mod_perl In reply to
You should look for:

- Any BEGIN or END blocks will not get run each request.
- All global variables will remember their state. This is a big one. Make sure you either initilize any globals, or you don't use them.
- Don't my() lexical variables at file scope (i.e. outside of a sub). They get put into a sub by Apache::Registry which will closure the variable, causing a lot of confusion.

Other then that, apart from checking it works, you should check that it doesn't leak memory. Watch the size of your httpd via `ps` and then do a couple hundred requests to your script, and see if it's growing memory.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] mod_perl In reply to
Thanks for the tips...

>>Don't my() lexical variables at file scope (i.e. outside of a sub).<<

Eww no I don't do that, I do:

use vars qw/$A $B $C/;

...that will be ok I assume?...or are they the ones I need to look at resetting?

Last edited by:

Paul: Jun 12, 2002, 9:55 AM
Quote Reply
Re: [Paul] mod_perl In reply to
Nope, use vars is fine. It's:

my $foo = 5;
main();
sub main {
print "foo is $foo\n";
}

which will cause problems, as Apache::Registry wraps that in a sub:

sub {
my $foo = 5;
main();
sub main {
print "foo is $foo\n";
}
}

as then it will cause a closure.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] mod_perl In reply to
Cool it seems like I'm on the right track then.

Thanks.
Quote Reply
Re: [Alex] mod_perl In reply to
Hmm when I try to use:

use CGI qw(-compile :all)

....in my startup file I get:

subroutine div redefined at e:/perl/lib/Exporter.pm line 57

When I change back to:

use CGI;

...it works ok :(

Last edited by:

Paul: Jun 18, 2002, 3:49 AM