Home arrow Perl Programming arrow Page 2 - Subroutines in Perl

Homonyms - Perl

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.

  1. Subroutines in Perl
  2. Homonyms
  3. Argument Lists
  4. Named Arguments
By: O'Reilly Media
Rating: starstarstarstarstar / 10
August 16, 2007

print this article



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 Programming Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: