ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
12.11. Overriding Built-In FunctionsProblemYou want to replace a standard, built-in function with your own version. SolutionImport that function from another module into your own namespace. DiscussionMany (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 A standard Perl module that does this is Cwd, which can overload 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 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 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 AlsoThe section on "Overriding Built-in Functions" in Chapter 5 of Programming Perl and in perlsub (1) |