Perl Cookbook

Perl CookbookSearch this book
Previous: 12.10. Speeding Up Module Loading with AutoloaderChapter 12
Packages, Libraries, and Modules
Next: 12.12. Reporting Errors and Warnings Like Built-Ins
 

12.11. Overriding Built-In Functions

Problem

You want to replace a standard, built-in function with your own version.

Solution

Import that function from another module into your own namespace.

Discussion

Many (but not all) of Perl's built-in functions may be overridden. This is not something to be attempted lightly, but it is possible. You might do this, for example, if you are running on a platform that doesn't support the function that you'd like to emulate. Or, you might want to add your own wrapper around the built-in.

Not all reserved words have the same status. Those that return a negative number in the C-language keyword() function in the toke.c file in your Perl source kit may be overridden. Keywords that cannot be overridden as of 5.004 are chop, defined, delete, do, dump, each, else, elsif, eval, exists, for, foreach, format, glob, goto, grep, if, keys, last, local, m, map, my, next, no, package, pop, pos, print, printf, prototype, push, q, qq, qw, qx, redo, return, s, scalar, shift, sort, splice, split, study, sub, tie, tied, tr, undef, unless, unshift, untie, until, use, while, and y. The rest can.

A standard Perl module that does this is Cwd, which can overload chdir. Others are the by-name versions of the functions returning lists: File::stat, Net::hostent, Net::netent, Net::protoent, Net::servent, Time::gmtime, Time::localtime, Time::tm, User::grent, and User::pwent. These modules all override built-in functions like stat or getpwnam to return an object that can be accessed using a name, like getpwnam("daemon")->dir. To do this, they have to override the original, list-returning versions of those functions.

Overriding may be done uniquely by importing the function from another package. This import only takes effect in the importing package, not in all possible packages. It's not enough simply to predeclare the function. You have to import it. This is a guard against accidentally redefining built-ins.

Let's say that you'd like to replace the built-in time function, whose answer is in integer seconds, with one that returns a floating point number instead. You could make a FineTime module with an optionally exported time function as follows:

package FineTime;
use strict;
require Exporter;
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(time);

sub time() { ..... }  # TBA

Then the user who wants to use this augmented version of time would say something like:

use FineTime qw(time);
$start = time();
1 while print time() - $start, "\n";

This code assumes that your system has a function you can stick in the "TBA" definition above. See Recipe 12.14 for strategies that may work on your system.

For overriding of methods and operators, see Chapter 13.

See Also

The section on "Overriding Built-in Functions" in Chapter 5 of Programming Perl and in perlsub (1)


Previous: 12.10. Speeding Up Module Loading with AutoloaderPerl CookbookNext: 12.12. Reporting Errors and Warnings Like Built-Ins
12.10. Speeding Up Module Loading with AutoloaderBook Index12.12. Reporting Errors and Warnings Like Built-Ins