Now that you have mod_perl installed, you can move on to learning how to use it. This article will run you through some basic Perl concepts to start with.
Special Perl variables like $| (buffering), $^T (script's starttime), $^W (warnings mode), $/ (input record separator), $\(output record separator) and many more are all true global variables;they do not belong to any particular package (not even main::) andare universally available. This means that if you change them, youchange them anywhere across the entire program; furthermore you cannotscope them with my(). However you can local()ise them which means thatany changes you apply will only last until the end of the enclosingscope. In the mod_perl situation where the child server doesn'tusually exit, if in one of your scripts you modify a global variableit will be changed for the rest of the process' life and will affectall the scripts executed by the same process. Therefore localisingthese variables is highly recommended, I'd say mandatory.
I will demonstrate the case on the input record separator variable. Ifyou undefine this variable, the diamond operator (readline) will suckin the whole file at once if you have enough memory. Remembering thisyou should never write code like the example below.
$/ = undef; # BAD!
open IN, "file" ....
# slurp it all into a variable
$all_the_file = <IN>;
The proper way is to have a local() keyword before the specialvariable is changed, like this:
local $/ = undef;
open IN, "file" ....
# slurp it all inside a variable
$all_the_file = <IN>;
But there is a catch. local() will propagate the changed value to thecode below it. The modified value will be in effect until the scriptterminates, unless it is changed again somewhere else in the script.
A cleaner approach is to enclose the whole of the code that isaffected by the modified variable in a block, like this:
{
local $/ = undef;
open IN, "file" ....
# slurp it all inside a variable
$all_the_file = <IN>;
}
That way when Perl leaves the block it restores the original value ofthe $/ variable, and you don't need to worry elsewhere in yourprogram about its value being changed here.
Note that if you call a subroutine after you've set a global variablebut within the enclosing block, the global variable will be visiblewith its new value inside the subroutine.