Perl
  Home arrow Perl arrow Page 3 - 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? 
Google.com  
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 - Scalar Return Values
    ( Page 3 of 4 )


    Always return scalar in scalar returns.


    One of the more subtle features of Perl subroutines is the way that their call context propagates to their return statements. In most places in Perl, the context (list, scalar, or void) can be deduced at compile time. One place where it can’t be determined in advance is to the right of a return. The argument of a return is evaluated in what ever context the subroutine itself was called.

    That’s a very handy feature, which makes it easy to factor out or rename specific uses of built-in functions. For example, if you found yourself repeatedly filtering undefined and negative values out of lists:

      @valid_samples = grep {defined($_) && $_ >= 0} @raw_samples;

    it would be better to encapsulate that complex filter and rename it more meaningfully:

      sub valid_samples_in {
         
    return grep {defined($_) && $_ >= 0} @_;
      }

      # and then...

      @valid_samples = valid_samples_in(@raw_samples);

    Because the return expression is always evaluated in the same context as the surrounding call, it’s also still okay to use this subroutine in scalar context:

      if (valid_samples_in(@raw_samples) < $MIN_SAMPLE_COUNT) {
                  
    report_sensor_malfunction();
      }

    When the subroutine is called in scalar context, its return statement imposes scalar context on the grep, which then returns the total number of valid samples—just as a raw grep would do in the same position.

    Unfortunately, it’s easy to forget about the contextual lycanthropy of a return , espe cially when you write a subroutine that is “only ever going to be used one way”*. For example:

      sub how_many_defined {
         
    return grep {defined $_} @_ ;
      }

      # and "always" thereafter:

      my $found = how_many_defined(@raw_samples);

    But eventually someone will write:

      my ($found) = how_many_defined(@raw_samples);

    and introduce a very subtle bug. The parentheses around $found put it in a list con text, which puts the call to how_many_defined() in a list context, which puts the grep inside how_many_defined() in a list context, which causes the return to return the list of defined samples, the first of which is then assigned to $found †.

    If there were even the slightest chance that this scalar-returning subroutine might ever be called in a list context, it should have been written as follows:

      sub how_many_defined {
         
    return scalar grep {defined $_} @_;
      }

    There is no shame in using an explicit scalar anywhere you know you want a scalar but you’re not confident of your context. And because you can never be confident of your context in a return statement, an explicit scalar is always acceptable there.

    At very least, you should always add one anywhere that a previously mistaken expec tation regarding context has already bitten you. That way, the same misconception won’t bite whoever is eventually responsible for the care and feeding of your code (that is, most likely you again, six months later).



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek