Perl
  Home arrow Perl arrow Page 7 - Perl 101 (Part 5) - Sub-Zero Code
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Perl 101 (Part 5) - Sub-Zero Code - The Age Gauge
    (Page 7 of 7 )

    So Perl gives you "public" variables and "private" variables - more than enough for most programmers. But you know what geeks are like...they're never satisfied. And so Perl also provides a useful middle ground - variables which are available between subroutines, but are hidden from the main program.

    Why would you want to use something like this? Well, consider the following example, which demonstrates the concept:

    #!/usr/bin/perl # define some subroutines sub display_value { print "During the subroutine...you are $age years old.\n"; } sub change_age { local ($age) = $age + $increment; &display_value($age); } # ask for age print "How old are you?\n"; $age = ; chomp ($age); # ask for increment print "How many years would you like to add?\n"; $increment = ; chomp ($increment); # demonstrate local variable print "Before invoking the subroutine...you are $age years old.\n"; &change_age; print "After invoking the subroutine...you are $age years old.\n";
    And here's what it looks like:
    How old are you?
    32
    How many years would you like to add?
    9
    Before invoking the subroutine...you are 32 years old.
    During the subroutine...you are 41 years old.
    After invoking the subroutine...you are 32 years old.

    When making calls between subroutines in this manner, it often becomes necessary to store the value of a variable across subroutines - and that's where local() comes in. In the example above, the variable $age is assigned an initial [global] value on the basis of user input. However, once the &change_age subroutine is invoked, this global value is stored and a new value is assigned to $age.

    So far so good...we've already seen this with my(). But now, &change_age needs to call &display_value, and pass it the value of the variable $age. By declaring $age to be a "local" variable, Perl makes it possible for the &display_value subroutine to access the new value of $age, and display it.

    Once the subroutines finish and return control to the main program, the original value of $age is restored, and displayed. Thus, the example demonstrates how the local() keyword can be used to share variable values between subroutines, without affecting the global value of the variable.

    And that's about it for this week. Next time, we'll be taking a close look at some of Perl's built-in string, math and pattern-recognition functions...so make sure you come back!

    This article copyright Melonfire 2000. All rights reserved.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway