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

Subroutines in Perl
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-08-16


    Table of Contents:
  • Subroutines in Perl
  • Homonyms
  • Argument Lists
  • Named Arguments

  • 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


    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 (like lock ), 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 main tain 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
     

       

    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