Home arrow Perl Programming arrow Perl Subroutines: Arguments and Values

Perl Subroutines: Arguments and Values

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



Missing Arguments

Use definedness or existence to test for missing arguments.

It’s a common mistake to use a boolean test to probe for missing arguments:

  Readonly my $FILLED_USAGE => 'Usage: filled($text, $cols, $filler)';

  sub filled {
      my ($text, $cols, $filler) = @_;

      croak $FILLED_USAGE
          if !$text || !$cols || !$filler;

      # [etc.]

The problem is that this approach can fail in subtle ways. If, for example, the filler character is'0'or the text to be padded is an empty string, then an exception will incorrectly be thrown.

A much more robust approach is to test for definedness:

  use List::MoreUtils qw( any );

  sub filled {
      my ($text, $cols, $filler) = @_;

      croak $FILLED_USAGE
          if any {!defined $_} $text, $cols, $filler;

      # [etc.]

Or, if a particular number of arguments is required, and undef is an acceptable value for one of them, test for mere existence:

  sub filled {
      croak $FILLED_USAGE if @_ != 3; # All three args must be supplied

      my ($text, $cols, $filler) = @_;
# etc.

Existence tests are particularly efficient because they can be applied before the argument list is even unpacked. Testing for the existence of arguments also promotes more robust coding, in that it prevents callers from carelessly omitting a required argument, and from accidentally providing any extras.

Note that existence tests can also be used when some arguments are optional, because the recommended practice for this case—passing options in a hash—ensures that the actual number of arguments passed is fixed (or fixed-minus-one, if the options hash happens to be omitted entirely):

  sub filled {
      croak $FILLED_USAGE if @_ < 1 || @_ > 2;

      my ($text, $opt_ref) = @_;  # Cols and fill char now passed as options

      # etc.

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