eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak


introduction to Perl 5 for web developers
Variable Localization  
In Perl 4 there were two ways to define the package scope of a variable. First, the variable might have global scope.

Global scope means that the variable could be seen (accessible/modifiable) by every other object in the program. Sometimes this is a good thing. Sometimes you have variables, for example, whose values are relevant to every aspect of your program.

However, most times, it is better to limit the scope of variables to specific areas in your program. Limiting the scope helps maintain name space, reduce programmatic complexity, and increases modularity of design.

To limit the scope of a variable, Perl initially provided the "local" keyword. The local keyword was used to limit the scope of a variable to within its enclosing brackets

Consider the following example. Notice that when the local keyword is applied to the variable $a, changes made to $a inside of the subroutine have no affect on the value of $a outside of the subroutine. On the other hand, $b is not declared local in the subroutine and hence, changes made to $b in the subroutine affect $b outside of the subroutine:

[Localization Example]

In Perl 5, localization is handled by the "my" keyword which hides the variable from the outside world completely.

Hey, isn't that what local was supposed to do?

Well, not exactly. The local keyword is meant to assure "dynamic" scoping whereas the "my" keyword is for "lexical" scoping.

The big difference is that dynamically scoped variables are visible not only from within the enclosing brackets but in all subroutines called within those brackets. Lexical variables are totally hidden. Consider the following examples. Notice in particular that when "my" is applied to $a, the second subroutine cannot see the change whereas when "local" is applied, it can.

[Localization Example 2]

[Localization Example 3]

Another aspect of localizing Perl 5 variables is understanding how to make a variable local within a package or file but not within the scope of the whole program. In small programs, global variables can be much easier to deal with instead of having to recall and type the calling conventions of every defined subroutine.

Of course, we recommend local variables for the reasons stated earlier, but in those cases where strict adherence to local variables is painful, Perl supports a kind of compromise between global and local variables. Perl supports the concept of package-level and file-level variables.

Using the "my" keyword in a file outside the context of a subroutine will do this. Likewise if you wish to declare a variable that is local to a package, you can use the "use vars" pragma as shown below:

use vars qw($package_global_variable);

One caveat to using my to declare global variables is that you should be careful if you use them in conjunction with scoping tricks. In particular, one complex trick is called a "closure". Closure's are beyond the scope of this document, you can find it by using the perldoc command to read the perlfaq7 or the perlref document using the following syntax on your command-line (DOS for Windows, shell for UNIX users):

perldoc perlfaq7
perldoc perlref

Although local() is a Perl 4 keyword and in many cases, the my keyword is better for localizing variables in a manner that most programmers expect, local() does still have some uses in Perl 5. Talking about these uses is beyond the scope of this document but you can get more information about localization using the local keyword from the MJD article at http://perl.plover.com/local.html.

Previous | Next | Table of Contents