|laziness, impatience, and hubris|
DWIM: autoloading classesby Ovid (Cardinal)
|on Sep 17, 2003 at 00:35 UTC||Need Help??|
Update: How bizarre! I haven't been on Perlmonks all day, but I come here to make this weird post only to discover (after I post) a freakishly similar idea discussed today.
An interesting talk with a coworker about Smalltalk has inspired me to think about a new, rather curious module.
Often, when working on a project, I have many related packages. It can get awfully annoying to find this at the top of all of my code:
In Smalltalk, you just use (as in "make use of", not to be confused with Perl's keyword "use") the classes. In Java, there is at least a useful import keyword:import Foo.*;
I started to wonder why I can't do that in Perl. After talking to him about implementations, I took a snippet he wrote and came up with something like the following code:
If you don't understand that code, what it does is create a function in the "HTML::TokeParser" namespace that undefs itself (so it won't be called more than once), uses the class in question and then returns the fully qualified name as a string.
So what if I were to create a module that did something like this:
In other words, we can skip the long lists of "use" and require declarations. I specifically mention "Class" in the package name as I expect that function oriented modules would be more difficult to manage due to exporting issues.
Another interface -- but more dangerous since it could have namespace collisions if you use subs with upper-case first letters:
Can anyone think of strong reasons why this would or would not be a good idea? I can see some problems with this, but it's an interesting idea.
New address of my CGI Course.