Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Wht is the difference between CGI and Mod_Perl

by veeruch (Sexton)
on Jan 02, 2007 at 10:44 UTC ( [id://592524]=perlquestion: print w/replies, xml ) Need Help??

veeruch has asked for the wisdom of the Perl Monks concerning the following question:

hi Monks,
Happy New Year .wht is difference between CGI and Mod_perl.
  • Comment on Wht is the difference between CGI and Mod_Perl

Replies are listed 'Best First'.
Re: Wht is the difference between CGI and Mod_Perl
by themage (Friar) on Jan 02, 2007 at 11:01 UTC
    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.

      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.
Re: Wht is the difference between CGI and Mod_Perl
by perrin (Chancellor) on Jan 02, 2007 at 15:58 UTC

    The basic differences are the process model and the API. The process model for CGI is to fork a new perl interpreter every time a request comes in. For mod_perl, the interpreter is loaded into the web server process and stays there, which means there is no fork, and your scripts can stay in memory, and you can do things like keep database connections open to save time.

    The other difference is the API. You have full access to the Apache API from mod_perl, which means you can do anything that other Apache modules in C can do: apply some filtering after a PHP script runs, use a custom data source for authentication, do a custom proxy to some internal server, etc.

Re: Wht is the difference between CGI and Mod_Perl
by Anonymous Monk on Jan 02, 2007 at 10:54 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://592524]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found