Perl
  Home arrow Perl arrow Perl Subroutines: Arguments and Values
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl Subroutines: Arguments and Values
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-08-23

    Table of Contents:
  • Perl Subroutines: Arguments and Values
  • Default Argument Values
  • Scalar Return Values
  • Contextual Return Values

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Perl Subroutines: Arguments and Values
    (Page 1 of 4 )

    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.

    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 Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Perl Best Practices," published by...
     

    Buy this book now. This article is excerpted from chapter nine of the book Perl Best Practices, written by Damian Conway (O'Reilly; ISBN: 0596001738). Check it out today at your favorite bookstore. Buy this book now.

       

    PERL ARTICLES

    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks
    - Perl: Releasing Your Inner Textuality




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway