12.8 Importing with ExporterEarlier we skipped over that "and now magic happens" part where the import routine (defined by the module author) is supposed to take File::Basename::fileparse and somehow alias it into the caller's package so it's callable as fileparse.Perl provides a lot of introspection capabilities. Specifically, you can look at the symbol table (where all subroutines and most variables are named), see what is defined, and alter those definitions. You saw a bit of that back in the AUTOLOAD mechanism earlier. In fact, as the author of File::Basename, if you simply want to force filename, basename, and fileparse from the current package into the main package, you can write import like this: sub import {Boy, is that cryptic! And limited. What if you didn't want fileparse? What if you invoked use in a package other than main?Thankfully, there's a standard import that's available in the Exporter module. As the module author, all you do is add: use Exporter; Now the import call to the package will inherit upward to the Exporter class, providing an import routine that knows how to take a list of subroutines[12] and export them to the caller's package.[12] And variables, although far less common, and arguably the wrong thing to do. |
•
Table of Contents
•
Index
•
Reviews
•
Reader Reviews
•
Errata
Learning Perl Objects, References & Modules
By
Randal L. Schwartz