Perl
  Home arrow Perl arrow Page 2 - Perl Subroutines: Arguments and Values
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 3
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT