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.


In reply to Re^2: Wht is the difference between CGI and Mod_Perl by bart
in thread Wht is the difference between CGI and Mod_Perl by veeruch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.