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

Turning Up The Heat - 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
Now, if you've been paying attention, you've seen how subroutines can help you segregate blocks of code, and use the same piece of code over and over again, thereby eliminating unnecessary duplication. But this is just the tip of the iceberg...

The subroutines you've seen thus far are largely static, in that the variables they use are already defined. But it's also possible to pass variables to a subroutine from the main program - these variables are called "arguments", and they add a whole new level of power and flexibility to your code.

Consider the following simple example:

#!/usr/bin/perl # define a subroutine sub add_two_numbers { $sum = $_[0] + $_[1]; return $sum; } $total = &add_two_numbers(3,5); print "The sum of the numbers is $total\n";
A few words of explanation here:

You're already familiar with the special Perl array @ARGV, which contains parameters passed to the Perl program on the command line. Well, Perl also has a variable named @_, which contains arguments passed to a subroutine, and which is available to the subroutine when it is invoked. The value of each element of the array can be accessed using standard scalar notation - $_[0] for the first element, $_[1] for the second element, and so on.

In the example above, once the &add_two_numbers subroutine is invoked with the numbers 3 and 5, the numbers are transferred to the @_ variable, and are then accessed using standard scalar notation within the subroutine. Once the addition has been performed, the result is returned to the main program, and displayed on the screen via the print() statement.

Note the manner in which arguments are passed to the subroutine, enclosed within a pair of parentheses.

How about something a little more useful? Let's go back a couple of pages, and consider the &change_temp subroutine we've defined:

#!/usr/bin/perl # define a subroutine sub change_temp { $celsius = 35; $fahrenheit = ($celsius * 1.8) + 32; } # assign return value to variable $result = &change_temp; print "35 Celsius is $result Fahrenheit\n";
Now, suppose we alter this to accept the temperature in Celsius from the main program, and return the temperature in Fahrenheit.

#!/usr/bin/perl # define a subroutine sub change_temp { $fahrenheit = ($_[0] * 1.8) + 32; } print "Enter temperature in Celsius\n"; $temperature = ; chomp ($temperature); $result = &change_temp($temperature); print "$temperature Celsius is $result Fahrenheit\n";
And here's what it would look like:
Enter temperature in Celsius
45
45 Celsius is 113 Fahrenheit

Take it one step further - how about allowing the user to specify the temperature to be converted on the command line itself?

#!/usr/bin/perl # define a subroutine sub change_temp { $fahrenheit = ($_[0] * 1.8) + 32; } # get the command-line parameters # and pass them to the subroutine # and assign the result $result = &change_temp(@ARGV); # print the result print "$ARGV[0] Celsius is $result Fahrenheit\n";
If you saved this program as "convert_temp.pl", and ran it like this

$ convert_temp.pl 35
you'd see
35 Celsius is 95 Fahrenheit

The above example also neatly demonstrates the relationship between @ARGV and @_ - the temperature entered on the command line first goes into the @ARGV variable, and is then passed to the subroutine via the @_ variable. Remember that the @_ variable is only available within the scope of a specific subroutine.

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: