Perl
  Home arrow Perl arrow Returns and Perl Subroutines
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

Returns and Perl Subroutines
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2007-08-29


    Table of Contents:
  • Returns and Perl Subroutines
  • Prototypes
  • Implicit Returns
  • Returning Failure

  • 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


    Returns and Perl Subroutines
    ( Page 1 of 4 )

    In this final part of a three part series covering subroutines in Perl, we will discuss returns and return values, as well as prototypes. This article 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.

    Multi-Contextual Return Values


    When there is no “obvious” scalar context return value, consider Contextual::Return instead.


    Sometimes no single scalar return value is appropriate for a list-returning subroutine. Your play-testers simply can’t agree: different developers consistently expect different behaviours in different scalar contexts.

    For example, suppose you’re implementing a get_server_status() subroutine that normally returns its information as a heterogeneous list:

      # In list context, return all the available information...
      my ($name, $uptime, $load, $users) = get_server_status($server_ID);

    You may find that, in scalar contexts, some programmers expected it to return its numeric load value:

      # Total load is sum of individual server loads...
     
    $total_load += get_server_status($server_ID);

    Others assumed it would return a boolean value indicating whether the server is up:

      # Skip inactive servers...
     
    next SERVER if ! get_server_status($server_ID);

    Still others anticipated a string summarizing the current status:

      # Compile report on all servers...
      $servers_summary .= get_server_status($server_ID) . "\n";

    While a fourth group hoped for a hash-reference, to give them convenient named access to the particular server information they wanted:

      # Total users is sum of users on each server...
     
    $total_users += get_server_status($server_ID)->{users};

    In such cases, implementing any one of these four expectations is going to leave three-quarters of your developers unhappy.

    At some point, every subroutine will be called in scalar context, and will have to return something. If that something isn’t obvious to the majority of people, then inexperienced developers—who might not even realize their call is in scalar context—will suffer. And experienced developers will suffer too: ham-strung by the limitations of scalar context return and forced to work with your arbitrary choice of return value.

    Perl’s subroutines are context-sensitive for a reason: so that they can Do The Right Thing when used in different ways. But often in scalar contexts there is no one Right Thing. So developers give up and just pick the One Thing That Seems Rightest... to them. All too often, a decision like that leads to confusion, frustration, and buggy code.

    Surprisingly, the underlying problem here isn’t that Perl is context-sensitive. The problem is that Perl isn’t context-sensitive enough.

    Perl has one kind of list context and one kind of void context, so simple list-context and void-context returns are the perfect tools for those. On the other hand, Perl has at least a dozen distinct scalar subcontexts: boolean, integer, floating-point, string, and the numerous reference types. So, unless one of those return types is the clear and obvious candidate, simple scalar context return is totally inadequate: a sledgehammer when you really need tweezers.

    Fortunately, there’s a simple way to allow subroutines like get_server_status() to cater for two or more different scalar-context expectations simultaneously. The Contextual::Return CPAN module provides a mechanism by which you can specify that a subroutine returns different scalar values in boolean, numeric, string, hash-ref, array-ref, and code-ref contexts. For example, to allow get_server_status() to simultaneously support all five return behaviours shown at the start of this guideline, you could simply write:

      use Contextual::Return;

      sub get_server_status {
          my ($server_ID) = @_;

          # Acquire server data somehow...
          my %server_data
              = _ascertain_server_status($server_ID);

          # Return different components of that data, depending on call context...
          return (
              LIST    { @server_data{ qw( name uptime load users ) };         }
              BOOL    { $server_data{uptime} > 0;                         }
              NUM     {
    $server_data{load};                   }
              STR     { "$server_data{name}: $server_data{uptime}, $server_data{load}";                  }
              HASHREF { \%server_data;    }
           );
       }

    Now, in a list context, get_server_status() uses a hash slice to extract the information in the expected order. In a boolean context, it returns true if the uptime is nonzero. In a numeric context, it returns the server load. In a string context, a string summarizing the server’s status is returned. And when the return value is expected to be a hash reference, get_server_status() simply returns a reference to the entire %server_data hash.

    Note that each of those alternative return values is lazily evaluated. That means, on any given call to get_server_status() , only one of the five contextual return blocks is actually executed.

    Even in cases where you don’t need to distinguish between so many alternatives, the Contextual::Return module can still improve the maintainability of your code, compared to using the built-in wantarray . The module allows you to say explicitly what you want to happen in different return context, and to label each of those outcomes with an obvious keyword. For example, suppose you had a subroutine such as:

      sub defined_samples_in {
          if (wantarray) {
              return grep {defined $_} @_;
          }

          return first {defined $_} @_;
      }

    Without changing its behaviour at all, you could make the code considerably more self-documenting, and emphasize the inherent symmetry of the list and scalar cases, by rewriting it with a single contextual return:

      use Contextual::Return;

      sub defined_samples_in {
         
    return (
             
    LIST   { grep {defined $_} @_ }
              SCALAR { first {defined $_} @_ }
         
    );
      }

    Besides producing more explicit and less cluttered code, this approach is more maintainable, too. When you need to extend the return behaviour of the subroutine, to more precisely match the expectations of those who use it, you can just add extra labeled return contexts, anywhere in the return list:

    use Contextual::Return;
    sub defined_samples_in {
    return (
    LIST { grep {defined $_} @_ } # All defined vals
    SCALAR { first {defined $_} @_ } # One defined val
    NUM { scalar grep {defined $_} @_ } # How many vals defined?
    ARRAYREF { [ grep {defined $_} @_ ] } # Return vals in an array
    );  
    }    

    Regardless of the order in which the alternatives appear, Contextual::Return will automatically select the most appropriate behaviour in each call context.



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT