Perl
  Home arrow Perl arrow 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 Rational Software Development Conference
 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Subroutines in Perl
    (Page 1 of 4 )

    Subroutines let programmers extend the Perl language...at least in theory. There are certain pitfalls for which you need to be alert. This article, the first of three parts, will warn you about those pitfalls and help you avoid them. It 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.

    If you have a procedure with ten parameters, you probably missed some. —Alan Perlis

    Subroutines are one of the two primary problem-decomposition tools available in Perl, modules being the other. They provide a convenient and familiar way to break a large task down into pieces that are small enough to understand, concise enough to implement, focused enough to test, and simple enough to debug.

    In effect, subroutines allow programmers to extend the Perl language, creating useful new behaviours with sensible names. Having written a subroutine, you can immediately forget about its internals, and focus solely on the abstracted process or function it implements.

    So the extensive use of subroutines helps to make a program more modular, which in turn makes it more robust and maintainable. Subroutines also make it possible to structure the actions of programs hierarchically, at increasingly high levels of abstraction, which improves the readability of the resulting code.

    That’s the theory, at least. In practice, there are plenty of ways that using subroutines can make code less robust, buggier, less concise, slower, and harder to understand. The guidelines in this chapter focus on avoiding those outcomes.

    Call Syntax


    Call subroutines with parentheses but without a leading &.


    It’s possible to call a subroutine without parentheses, if it has already been declared in the current namespace:

      sub coerce;

      # and later...

      my $expected_count = coerce $input, $INTEGER, $ROUND_ZERO;

    But that approach can quickly become much harder to understand:

      fix my $gaze, upon each %suspect;

    More importantly, leaving off the parentheses on subroutines makes them harder to distinguish from builtins, and therefore increases the mental search space when the reader is confronted with either type of construct. Your code will be easier to read and understand if the subroutines always use parentheses and the built-in functions always don’t:

      my $expected_count = coerce($input, $INTEGER, $ROUND_ZERO);

      fix(my $gaze, upon(each %suspect));

    Some programmers still prefer to call a subroutine using the ancient Perl 4 syntax, with an ampersand before the subroutine name:

      &coerce($input, $INTEGER, $ROUND_ZERO);

      &fix(my $gaze, &upon(each %suspect));

    Perl 5 does support that syntax, but nowadays it’s unnecessarily cluttered. Barewords is forbidden underuse strict, so there are far fewer situations in which a subroutine call has to be disambiguated.

    On the other hand, the ampersand itself is visually ambiguous; it can also signify a bitwise AND operator, depending on context. And context can be extremely subtle:

      $curr_pos  = tell &get_mask();    # means: tell(get_mask())
     
    $curr_time = time &get_mask();   # means: time() & get_mask(
    )

    Prefixing with&can also lead to other subtle (but radical) differences in behaviour:

      sub fix {
          my (@args) = @_ ? @_ : $_;   # Default to fixing $_ if no args provided

          # Fix each argument by grammatically transforming it and then printing it...
         
    for my $arg (@args) {
              
    $arg =~ s/\A the \b/some/xms;
             
    $arg =~ s/e \z/es/xms;
             
    print $arg;
          }

          return;
      }

      # and later...

      &fix('the race');   # Works as expected, prints: 'some races'
     
    for ('the gaze', 'the adhesive') {
         
    &fix;          # Doesn't work as expected: looks like it should fix($_),
                        # but actually means fix(@_), using this scope's @_!
                        # See the 'perlsub' manpage for details
     
    }

    All in all, it’s clearer, less ambiguous, and less error-prone to reserve the&subnamesyntax for taking references to named subroutines:

      set_error_handler( \&log_error );

    Just use parentheses to indicate a subroutine call:

      coerce($input, $INTEGER, $ROUND_ZERO);

      fix( my $gaze, upon(each %suspect) );

      $curr_pos  = tell get_mask();
      $curr_time = time & get_mask();

    And always use the parentheses when calling a subroutine, even when the subroutine takes no arguments (like get_mask()). That way it’s immediately obvious that you intend a subroutine call:

      curr_obj()->update($status);   # Call curr_obj() to get an object,
                                     # then call the update()method on that object

    and not a typename:

    curr_obj->update($status);           # Maybe the same (if currobj() already declared),
                                        # otherwise call update() on class 'curr_obj'

    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: 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
    - Perl: Releasing Your Inner Textuality




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