Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Variable Localization

September 28, 1998

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 increase modularity of design.

To limit package scope, 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]

Anonymous References
Introduction to Perl 5 | Table of Contents
Introduction to Object Oriented Design



Up to => Home / Authoring / Languages / Perl / 5




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers