Perl
  Home arrow Perl arrow Page 2 - 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 
 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

    Dell PowerEdge Servers

    Subroutines in Perl - Homonyms
    (Page 2 of 4 )

     


    Don’t give subroutines the same names as built-in functions.


    If you declare a subroutine with the same name as a built-in function, subsequent invocations of that name will still call the builtin...except when occasionally they don’t. For example:  

      sub lock {
         
    my ($file) = @_;
         
    return flock $file, LOCK_SH;
     
    }

      sub link {
         
    my ($text, $url) = @_;
         
    return qq{<a href="$url">$text</a>};
      }

      lock($file);                     # Calls 'lock' subroutine; built-in 'lock' hidden
     
    print link($text, $text_url);   
    # Calls built-in 'link'; 'link' subroutine hidden

    Perl considers some of its builtins (like link) to be “more built-in” than others (likelock), and chooses accordingly whether to call your subroutine of the same name. If the builtin is “strongly built-in”, an ambiguous call will invoke it, in preference to any subroutine of the same name. On the other hand, if the builtin is “weakly built-in”, an ambiguous call will invoke the subroutine of the same name instead.

    Even if these subroutines did always work as expected, it’s simply too hard to maintain code where the program-specific subroutines and the language’s keywords overlap:

      sub crypt { return "You're in the tomb of @_\n"    }
      sub map   { return "You have found a map of @_\n"  }
      sub chop  { return "You have chopped @_\n"     }
      sub close { return "The @_ is now closed\n" }
      sub hex   { return "A hex has been cast on @_\n"     }

      print crypt( qw( Vlad Tsepes ) );        # Subroutine or builtin?

      for my $reward (qw( treasure danger) ) {
         
    print map($reward, 'in', $location); # Subroutine or builtin
     
    }

      print hex('the Demon');                  # Subroutine or builtin
     
    print chop('the Demon');                 # Subroutine or builtin
    ?

    There is an inexhaustible supply of subroutine names available; names that are more descriptive and unambiguous. Use them:

      sub in_crypt        { return "You're in the tomb of @_\n"  }
      sub find_map        { return "You have found a map of @_\n"  }
      sub chop_at         { return "You have chopped @_\n"    }
      sub close_the       { return "The @_ is now closed\n"      }
      sub hex_upon        { return "A hex has been cast on @_\n" }

      print in_crypt( qw( Vlad Tsepes ) );

      for my $reward (qw( treasure danger )) {
         
    print find_map($reward, 'in', $location);
      }

      print hex_upon('the Demon');
      print chop_at('the Demon');

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