Home arrow Perl Programming arrow Page 3 - Perl Subroutines: Arguments and Values

Scalar Return Values - Perl

In this second part of a three-part series covering subroutines in Perl, you will learn about missing arguments, default argument values, and more. It is excerpted from chapter nine of the book Perl Best Practices, written by Damian Conway (O'Reilly; ISBN: 0596001738). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Perl Subroutines: Arguments and Values
  2. Default Argument Values
  3. Scalar Return Values
  4. Contextual Return Values
By: O'Reilly Media
Rating: starstarstarstarstar / 5
August 23, 2007

print this article



Always return scalar in scalar returns.

One of the more subtle features of Perl subroutines is the way that their call context propagates to their return statements. In most places in Perl, the context (list, scalar, or void) can be deduced at compile time. One place where it can’t be determined in advance is to the right of a return. The argument of areturnis evaluated in whatever context the subroutine itself was called.

That’s a very handy feature, which makes it easy to factor out or rename specific uses of built-in functions. For example, if you found yourself repeatedly filtering undefined and negative values out of lists:

  @valid_samples = grep {defined($_) && $_ >= 0} @raw_samples;

it would be better to encapsulate that complex filter and rename it more meaningfully:

  sub valid_samples_in {
return grep {defined($_) && $_ >= 0} @_;

  # and then...

  @valid_samples = valid_samples_in(@raw_samples);

Because the return expression is always evaluated in the same context as the surrounding call, it’s also still okay to use this subroutine in scalar context:

  if (valid_samples_in(@raw_samples) < $MIN_SAMPLE_COUNT) {

When the subroutine is called in scalar context, its return statement imposes scalar context on the grep, which then returns the total number of valid samples—just as a raw grep would do in the same position.

Unfortunately, it’s easy to forget about the contextual lycanthropy of areturn, especially when you write a subroutine that is “only ever going to be used one way”*. For example:

  sub how_many_defined {
return grep {defined $_} @_;

  # and "always" thereafter:

  my $found = how_many_defined(@raw_samples);

But eventually someone will write:

  my ($found) = how_many_defined(@raw_samples);

and introduce a very subtle bug. The parentheses around$foundput it in a list context, which puts the call tohow_many_defined()in a list context, which puts thegrep insidehow_many_defined()in a list context, which causes thereturn to return the list of defined samples, the first of which is then assigned to$found†.

If there were even the slightest chance that this scalar-returning subroutine might ever be called in a list context, it should have been written as follows:

  sub how_many_defined {
return scalar grep {defined $_} @_;

There is no shame in using an explicit scalar anywhere you know you want a scalar but you’re not confident of your context. And because you can never be confident of your context in a return statement, an explicit scalar is always acceptable there.

At very least, you should always add one anywhere that a previously mistaken expectation regarding context has already bitten you. That way, the same misconception won’t bite whoever is eventually responsible for the care and feeding of your code (that is, most likely you again, six months later).

>>> More Perl Programming Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: