Perl
  Home arrow Perl arrow Page 6 - Perl 101 (Part 5) - Sub-Zero Code
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (Part 5) - Sub-Zero Code
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2000-07-21


    Table of Contents:
  • Perl 101 (Part 5) - Sub-Zero Code
  • Great Movies...
  • ...And Memorable Friends
  • Popping The Question
  • Turning Up The Heat
  • My() Hero!
  • The Age Gauge

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (Part 5) - Sub-Zero Code - My() Hero!
    ( Page 6 of 7 )

    Let's now talk a little bit about the variables used within a subroutine, and their relationship with variables in the main program. Unless you specify otherwise, the variables used within a subroutine are global - that is, the values assigned to them are available throughout the program, and changes made to them during subroutine execution are not restricted to the subroutine space alone.

    For a clearer example of what this means, consider this simple example:

    #!/usr/bin/perl # define a subroutine sub change_value { $hero = "Wolverine"; } # define a variable $hero = "The Incredible Hulk"; # before invoking subroutine print "Today's superhero is $hero\n"; print "Actually, I've changed my mind..."; &change_value; # after invoking subroutine print "...gimme $hero instead.\n";
    And here's what you'll see:
    Today's superhero is The Incredible Hulk
    Actually, I've changed my mind......gimme Wolverine instead.

    Obviously, this is not always what you want - there are numerous situations where you'd prefer the variables within a subroutine to remain "private", and not disturb the variables within the main program. And this is precisely the reason for Perl's my() construct.

    The my() construct allows you to define variables whose influence does not extend outside the scope of the subroutine within which they are enclosed. Take a look:

    #!/usr/bin/perl # define a subroutine sub change_value { # this statement added my ($hero); $hero = "Wolverine"; } # define a variable $hero = "The Incredible Hulk"; # before invoking subroutine print "Today's superhero is $hero\n"; print "Actually, I've changed my mind..."; &change_value; # after invoking subroutine print "...gimme $hero instead.\n";
    And here's what you'll get:
    Today's superhero is The Incredible Hulk
    Actually, I've changed my mind......gimme The Incredible Hulk instead.

    What happens here? Well, when you define a variable with the "my" keyword, Perl first checks to see if a variable already exists with the same name. If it does [as in the example above], its value is stored and a new variable is created for the duration of the subroutine. Once the subroutine has completed its task, this new variable is destroyed and the previous value of the variable is restored.

    The my() operator can be used with both scalar and array variables. And - since Perl is all about efficiency - you can assign a value to the variable at the same that that you declare it, like this:

    sub change_value { my ($hero) = "Wolverine"; }


    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT