Perl Programming Page 7 - Introduction to mod_perl (part 4): Perl Basics |
Now let's talk about Special Perl Variables. Special Perl variables like 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 $/ = undef; open IN, "file" .... # slurp it all inside a variable $all_the_file = <IN>; But there is a catch. 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 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.
blog comments powered by Disqus |