On Fri, Feb 02, 2001 at 11:56:50AM -0600, Garrett Goebel wrote:
> Michael Schwern's AnyLoader is a bit strange though. To use an explicitly
> qualified function if the only perceivable gain were to allow you to skip
> needing an 'use'. After all, if the purpose is to mangle your namespace...
> why are you explicitly calling a function in the first place?  But that
> isn't the main reason for AnyLoader is it?

The main use of AnyLoader was to automate the lazy loading of modules
on demand without the problems of autouse.pm.

$ perl -wle 'use autouse "Carp" => qw(carp croak);  carp("foo")'
Subroutine carp redefined at /usr/lib/perl5/5.6/autouse.pm line 57.
foo at -e line 1

$ perl -wle 'use AnyLoader;  Carp::carp("foo")'
foo at -e line 1

The main use is for branches of code which are rarely executed, yet
required pulling in heavy modules.  Carp, for example (before it went
on a diet in 5.6.0).

    if( $some_error ) {
        require Carp;
        Carp::croak("AHHHH!");
    }

I got tired of writing "require Carp" all over my code.

Anyhow, the fully-qualified function name requirement can easily be
removed.  All I have to do is check to see if the namespace of the
called function matches the namespace of the caller.  That indicates
an unqualified, undefined function call (though doesn't guarantee it,
but this IS a prototype).  Then AnyLoader looks in a (as yet
undetermined) function registry (a glorified hash), loads the
appropriate module and exports the appropriate function.


PS Actually, it REALLY exists because Arnar came up with a neat trick
and I ran with it.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
The eye opening delightful morning taste of expired cheese bits in sour milk!

Reply via email to