Perl
  Home arrow Perl arrow Page 3 - Subroutines in Perl
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 Developerworks
 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

Subroutines in Perl
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-08-16

    Table of Contents:
  • Subroutines in Perl
  • Homonyms
  • Argument Lists
  • Named Arguments

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Subroutines in Perl - Argument Lists
    (Page 3 of 4 )


    Always unpack @_ first.


    Subroutines always receive their arguments in the @_ array. But accessing them via $_[0], $_[1], etc. directly is almost always a Very Bad Idea. For a start, it makes the code far less self-documenting:

      Readonly my $SPACE => q{ };

      # Pad a string with whitespace...
     
    sub padded {
         
    # Compute the left and right indents required...
         
    my $gap   = $_[1] - length $_[0];
          my $left  = $_[2] ? int($gap/2) : 0;
          my $right = $gap - $left;

          # Insert that many spaces fore and aft...
         
    return $SPACE x $left
               . $_[0]
               . $SPACE x $right;
     
    }

    Using “numbered parameters” like this makes it difficult to determine what each argument is used for, whether they’re being used in the correct order, and whether the computation they’re used in is algorithmically sane. Compare the previous version to this one:

      sub padded {
          my ($text, $cols_count, $want_centering) = @_;

          # Compute the left and right indents required...
         
    my $gap   = $cols_count - length $text;
          my $left  = $want_centering ? int($gap/2) : 0;
          my $right = $gap - $left;

          # Insert that many spaces fore and aft...
         
    return $SPACE x $left
               . $text
               . $SPACE x $right;
      }

    Here the first line unpacks the argument array to give each parameter a sensible name. In the process, that assignment also documents the expected order and intended purpose of each parameter. The sensible parameter names also make it easier to verify that the computation of $left and $right is correct.

    A mistake when using numbered parameters:

      my $gap   = $_[1] - length $_[2];
      my $left  = $_[0] ? int($gap/2) : 0;
      my $right = $gap - $left;

    is much harder to identify than when named variables are in the wrong places:

      my $gap   = $cols_count - length $want_centering;
      my $left  = $text ? int($gap/2) : 0;
      my $right = $gap - $left;

    Moreover, it’s easy to forget that each element of @_ is an alias for the original argument; that changing $_[0] changes the variable containing that argument:

      # Trim some text and put a "box" around it...
     
    sub boxed{
          $_[0] =~ s{\A \s+ | \s+ \z}{}gxms;
          return "[$_[0]]";
     
    }

    Unpacking the argument list creates a copy, so it’s far less likely that the original arguments will be inadvertently modified:

      # Trim some text and put a "box" around it...
     
    sub boxed
    {
          my ($text) = @_;

          $text =~ s{\A \s+ | \s+ \z}{}gxms;
          return "[$text]";
      }

    It’s acceptable to unpack the argument list using a single list assignment as the first line of the subroutine:

      sub padded {
          my ($text, $cols_count, $want_centering) = @_;

          # [Use parameters here, as before]
     
    }

    Alternatively, you can use a series of separate shift calls as the subroutine’s first “paragraph”:

      sub padded {
          my $text           = shift;
          my $cols_count     = shift;
          my $want_centering = shift;

          # [Use parameters here, as before]
     
    }

    The list-assignment version is more concise, and it keeps the parameters together in a horizontal list, which enhances readability, provided that the number of parameters is small.

    Theshift-based version is preferable, though, whenever one or more arguments has to be sanity-checked or needs to be documented with a trailing comment:

      sub padded {
          my $text           = _check_non_empty(shift);
          my $cols_count     = _limit_to_positive(shift);
          my $want_centering = shift;

          # [Use parameters here, as before]
     
    }

    Note the use of utility subroutines (see “Utility Subroutines” in Chapter 3) to perform the necessary argument verification and adjustment. Each such subroutine acts like a filter: it expects a single argument, checks it, and returns the argument value if the test succeeds. If the test fails, the verification subroutine may either return a default value instead, or call croak() to throw an exception (see Chapter 13). Because of that second possibility, verification subroutines should be defined in the same package as the subroutines whose arguments they are checking.

    This approach to argument verification produces very readable code, and scales well as the tests become more onerous. But it may be too expensive to use within small, frequently called subroutines, in which case the arguments should be unpacked in a list assignment and then tested directly:

      sub padded {
         
    my ($text, $cols_count, $want_centering) = @_;
         
    croak  q{Can't pad undefined text}    if !defined $text;
         
    croak qq{Can't pad to $cols_count columns} if $cols_count <= 0;

          # [Use parameters here, as before]
     
    }

    The only circumstances in which leaving a subroutine’s arguments in @_ is appropriate is when the subroutine:

    • Is short and simple
    • Clearly doesn’t modify its arguments in any way
    • Only refers to its arguments collectively (i.e., doesn’t index@_)
    • Refers to@_only a small number of times (preferably once) 
    • Needs to be efficient

    This is usually the case only in “wrapper” subroutines:

      # Implement the Perl 6 print+newline function...
     
    sub say {
         
    return print @_, "\n";
      }

      # and later...

      say( 'Hello world!' );
      say( 'Greetings to you, people of Earth!' );

    In this example, copying the contents of @_ to a lexical variable and then immediately passing those contents to print would be wasteful.

    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: A Continuing Look at Hashes and Multid...
    - 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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