Home arrow Perl Programming arrow Page 2 - Arguments and Return Values in Perl

Return Values - Perl

In this third part of a six-part article series on subroutines and functions in Perl, you'll learn about passing arguments into functions, and implicitly and explicitly getting return values. This article was excerpted from chapter six of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Arguments and Return Values in Perl
  2. Return Values
By: Apress Publishing
Rating: starstarstarstarstar / 0
April 11, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Sometimes we don’t want to perform an action like printing out the total, but instead we want to return the total. We may also want to return a result to indicate whether what we were doing succeeded. This will allow us to say things like

$sum_of_100 = total(1..100);

There are two ways to do this: implicitly or explicitly. The implicit way is nice and easy—we just make the value we want to return the last thing in our subroutine:

#!/usr/bin/perl -w
# total2.pl

use strict;

my $total = total(111, 107, 105, 114, 69);
print "the total is: $total\n";
my $sum_of_100 = total(1..100);
print "the sum of 100 is: $sum_of_100\n";

sub total {
my $total = 0;
$total += $_ foreach @_;
$total;
}

Running this code results in the following:

$ perl total2.pl
the total is: 506
the sum of 100 is: 5050
$

The last expression in the function doesn’t need to be a variable: we could use any expression. We can also return a list instead of a single scalar.

Here is an example of returning a list from a function. Let’s convert a number of seconds to hours, minutes, and seconds. We pass the time in seconds into the subroutine, and it returns a three-element list with the hours, minutes, and remaining seconds.

#!/usr/bin/perl -w
# seconds.pl

use strict;

my ($hours, $minutes, $seconds) = secs2hms(3723);
print "3723 seconds is $hours hours, $minutes minutes and $seconds seconds"; print "\n";

sub secs2hms {
my ($h,$m);
my $seconds = shift; # defaults to shifting @_
$h = int($seconds/(60*60));
$seconds %= 60*60;
$m = int($seconds/60);
$seconds %= 60;
($h,$m,$seconds);
}

This tells us that

$ perl seconds.pl
3723 seconds is 1 hours, 2 minutes and 3 seconds
$

This program illustrates that just like a built-in function, when we’re expecting a subroutine to return a list, we can use an array or list of variables to collect the return values:

my ($hours, $minutes, $seconds) = secs2hms(3723);

Whensecs2hms()returns, this will be equivalent to

my ($hours, $minutes, $seconds) = (1,2,3);

And now let’s look at how the subroutine works. We start in the usual way:sub, the name, and a block.

sub secs2hms {

We have two variables to represent hours and minutes, and we read the parameters in from@_ --if you don’t tellshift()which array to take data from, it’ll read from@_ if you’re in a subroutine or@ARGVif you’re not. Therefore, the first argument tosecs2hms(), 3723, is shifted into$seconds:

my ($h,$m);
my $seconds = shift;

Then the actual conversion: there are 3600 (60*60) seconds in an hour, and so the number of hours is the number of seconds divided by 3600. However, that’ll give us a floating point number—if we divided 3660 by 3600, we’d get 1.0341666. We’d rather have “one and a bit,” so we useint()to get the integer value, the “1” part of the division, and use the modulus operator to get the remainder; having dealt with the first 3600 seconds, we want to carry on looking at the next 123.

$h = int($seconds/(60*60));
$seconds %= 60*60;

The second statement sets$secondsto$seconds % (60*60)—if it was 3723 before, it’ll be 123 now.

The same goes for minutes: we divide to get “two and a bit,” and getting the remainder tells us that there are 3 seconds outstanding. Hence, our values are 1 hour, 2 minutes, and 3 seconds.

$m = int($seconds/60);
$seconds %= 60;

We return this just by leaving a list of the values as the last thing in the subroutine.

($h,$m,$seconds);

The return Statement

The explicit method of returning something from a subroutine is to sayreturn(...). The firstreturnstatement we come across will immediately return to the caller. For example:

sub secs2hms {
my ($h,$m);
my $seconds = shift;
$h = int($seconds/(60*60));
$seconds %= 60*60;
$m = int($seconds/60);
$seconds %= 60;
 
return ($h,$m,$seconds);
print "This statement is never reached.";

}

This also means we can have more than one return statement, and it’s often useful to do so.

Please check back for the next part of this series.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

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: