Home arrow Perl Programming arrow Page 6 - Perl 101 (Part 5) - Sub-Zero Code

My() Hero! - Perl

This week, Perl 101 introduces you to subroutines and teaches youhow to structure your code for maximum reusability. Also included: returnvalues, my() and local() constructs, and a marriage proposal.

TABLE OF CONTENTS:
  1. Perl 101 (Part 5) - Sub-Zero Code
  2. Great Movies...
  3. ...And Memorable Friends
  4. Popping The Question
  5. Turning Up The Heat
  6. My() Hero!
  7. The Age Gauge
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 7
July 21, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

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: