Perl
  Home arrow Perl arrow Page 2 - Perl Subroutines: Arguments and Values
FaxWave - Free Trial.
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 
 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

    Dell PowerEdge Servers

    Perl Subroutines: Arguments and Values - Default Argument Values
    (Page 2 of 4 )


    Resolve any default argument values as soon as @_ is unpacked.


    The fundamental rule of argument processing is: nothing happens in the subroutine until all the arguments are stable. Don’t, for example, add in defaults on the fly:  

      Readonly my $DEF_PAGE_WIDTH => 78;
      Readonly my $SPACE          => q{ };

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

          # Compute left and right spacings...
         
    my $gap   = ($arg_ref->{cols}||$DEF_PAGE_WIDTH) - length($text||=$EMPTY_STR);
          my $left  = $arg_ref->{centered} ? int($gap/2) : 0;
          my $right = $gap - $left;

          # Prepend and append space...
         
    my $filler = $arg_ref->{filler} || $SPACE;
          return $filler x $left . $text . $filler x $right;
      }

    Apart from making the gap computation much harder to read and to verify, using the||and||=operators to select default values is equivalent to testing for truth, and therefore much more prone to error on the edge cases (such as a'0'fill character).

    If default values are needed, set them up first. Separating out any initialization will make your code more readable, and simplifying the computational statements is likely to make them less buggy too:

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

          # Set defaults...

    #

    If option given...

    Use option

    Else default

    my $cols

    = exists $arg_ref->{cols}

    ? $arg_ref->{cols}

    : $DEF_PAGE_WIDTH;

    my $filler = exists $arg_ref->{filler} ? $arg_ref->{filler} : $SPACE;

          # Compute left and right spacings...
         
    my $gap   = $cols - length $text;
          my $left  = $arg_ref->{centered} ? int($gap/2) : 0;
          my $right = $gap - $left;

          # Prepend and append space...
         
    return $filler x $left . $text . $filler x $right;
      }

    If there are many defaults to set up, the cleanest way to do that is by factoring the defaults out into a table (i.e., a hash) and then pre-initializing the argument hash with that table, like so:

      Readonly my %PAD_DEFAULTS => (
          cols     => 78,
          centered => 0,
          filler   => $SPACE,
         
    # etc.
     
    );

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

          # Unpack optional arguments and set defaults...
         
    my %arg = ref $arg_ref eq 'HASH' ? (%PAD_DEFAULTS, %{$arg_ref})
                  :       %PAD_DEFAULTS;

          # Compute left and right spacings...
         
    my $gap   = $arg{cols} - length $text;
          my $left  = $arg{centered} ? int($gap/2) : 0;
          my $right = $gap - $left;

          # Prepend and append space...
         
    return $arg{filler} x $left . $text . $arg{filler} x $right;
      }

    When the %arg hash is initialized, the defaults are placed ahead of the arguments supplied by the caller ((%PAD_DEFAULTS, %{$arg_ref})). So the entries in the default table are assigned to %arg first. Those default values are then overwritten by any entries from $arg_ref.

    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 5 hosted by Hostway