views:

111

answers:

4

I was just wondering what is best for performance when making a web service in Perl.

Is it best to have as short as possible .pl script and put as much code as possible in a module that is used by the .pl script, or doesn't it impact performance if i don't use a module at all?

I am using mod_perl on a CentOS Linux box with perl 5.8.8.

+2  A: 

You better think in terms of maintainability. Go for modules. When you are using mod-perl, there is no need to worry about peformance issues this may cause.

innaM
+4  A: 

Since you're using mod_perl, there is no appreciable performance penalty in spreading your code across as many files as you like. Using modules, small, testable functions, and an object-oriented structure for your code will make it much more maintainable, reusable, and extensible. And you don't need a .pl script at all, since you're using mod_perl. Just do something like:

httpd.conf

PerlModule My::WebApp
<Location /app>
    SetHandler perl-script
    PerlHandler My::WebApp
</Location>

My/WebApp.pm

package My::WebApp;

use strict;
use warnings;

use Apache2::Const -compile => qw(OK);

sub handler { 
    my $r = shift;    # apache request object

    # do stuff

    return Apache2::Const::OK;
}

Using a web application framework makes it even easier. CGI::Application has excellent mod_perl support built in.

friedo
Its very clever but A) wields yourself to Apache and B) hides the handler code for no good reason and C) makes it nigh impossible to test.
Schwern
@Schwern, I don't understand what you mean. The OP said he's using mod_perl which implied Apache. What is "hiding the handler code?"
friedo
@Schwern, friedo didn't say anything about the rest of the code. All of the stuff in "# do something" is where they should use the separate modules they create. :)
brian d foy
+4  A: 

There is a performance benefit to writing modules with mod_perl. You can have Apache load modules at startup. That way they're compiled and ready to go when it forks off a new child. They can also do work at startup and share that work rather than each child having to do it over again. It also has the potential for the compiled code to reside in shared memory reducing memory footprint.

Here's some info about Apache 2.x and Apache 1.x. In Apache 2 your strategy differs some based on what worker model you're using.

But more important is that modules are easier to test, document and reuse.

*Caveat: its been a while since I've done performance optimization of mod_perl.*

Schwern
+3  A: 

No matter what you are doing, you should separate how people run your program from all of its functionality. Separate your code into modules where they are not tightly coupled to anything else that you are doing. The Apache specific portions of your application should only be a thin layer that handle the connection between the request and the rest of your code.

brian d foy