Home arrow Perl Programming arrow Page 7 - Introduction to mod_perl (part 4): Perl Basics

The Scope of the Special Perl Variables - Perl

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.

TABLE OF CONTENTS:
  1. Introduction to mod_perl (part 4): Perl Basics
  2. Using Global Variables and Sharing Them Between Modules/Packages
  3. Making Variables Global With strict Pragma On
  4. Using Exporter.pm to Share Global Variables
  5. Using the Perl Aliasing Feature to Share Global Variables
  6. Using Non-Hardcoded Configuration Module Names
  7. The Scope of the Special Perl Variables
  8. Compiled Regular Expressions
  9. References
By: Stas Bekman
Rating: starstarstarstarstar / 6
January 03, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now let's talk about Special Perl Variables.

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.



 
 
>>> More Perl Programming Articles          >>> More By Stas Bekman
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: