Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Wht is the difference between CGI and Mod_Perl

by themage (Friar)
on Jan 02, 2007 at 11:01 UTC ( #592529=note: print w/replies, xml ) Need Help??

in reply to Wht is the difference between CGI and Mod_Perl

Hi veeruch,

The main diference between ModPerl and perl scripts running as CGIs is that ModPerl compiles perl modules and scripts the first time they are run/used, and keeps them in memory, ready to be executed in the next request.

A perl script run as CGI is compiled everytime it is runned.

Another diference is that ModPerl can be used also to create apache modules in Perl, that will completly handle the requests as if a normal apache module they were.

  • Comment on Re: Wht is the difference between CGI and Mod_Perl

Replies are listed 'Best First'.
Re^2: Wht is the difference between CGI and Mod_Perl
by bart (Canon) on Jan 02, 2007 at 11:40 UTC
    There's a bit more to it...

    In mod_perl, scripts are loaded as subroutines. So all the scripts form a part of one larger, main script. That sounds somewhat dangerous, and it is.

    You can imagine the big source to look a bit like:

    sub script1 { # contents of script file 1 inserted here my $x; # this is problematic in mod_perl... sub foo { # ... } foo(); } sub script2 { # contents of script file 2 inserted here sub foo { # ... } foo(); }

    Nested subs are somewhat problematic in Perl, in that they form a closure around the data the first time the outer sub is called. If there's a lexical variable ($x) that is used inside an inner sub, then the next time the lexical $x will see a different variable, but the inner sub will use the same variable it saw in the first invocation of the outer sub. So the nested sub foo will, after the first time, see a different $x than the top level sub script1 does. That's a major PITA, and, IMHO, not how it is supposed to be. But P5P seems reluctant to "fix" it, probably because it's very hard to fix (and to get it right).

    Lesson to be learned: don't use top level lexicals in mod_perl. Use globals instead, but you best somehow restrict the effect of the scope to the script... I think local (and our or use vars, for strict) ought to do.

    A second consequence is that if you edit a script, then in CGI it'll immediately be picked up by the webserver, but in mod_perl you have either restart the server, or find some other way to force it to reload the script from file — I think there are a few tricks to make that happen.

      What you're talking about here is specific to the ModPerl::Registry module which is used for running CGI scripts under mod_perl. It does automatically pick up changes in your scripts. If you write an actual mod_perl module instead, you don't have to worry at all about it changing your code or creating nested subs.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://592529]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-08-07 19:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found