session has asked for the wisdom of the Perl Monks concerning the following question:

I have a very basic AUTOLOAD function to generate accessor methods for each of the 300 variables in my class. I'd like to export these methods so that the caller can simply use Function_Name() rather than $object->Function_Name(). Engineers, not programmers, are going to be maintaining this code and I'd like to make things easy as to avoid as many calls as possible.

Also, every time I generate an AUTOLOAD() function I add it to the symbol table like this:

*{$AUTOLOAD} = sub { return $_[0]->{$attr} };

How do you export AUTOLOADed functions, though? Exporting preloaded functions is easy but I can't quite figure out how to do the same for autoloaded ones.

Replies are listed 'Best First'.
Re: Exporting AUTOLOADed methods?
by Ovid (Cardinal) on Aug 12, 2002 at 20:09 UTC

    Ugh. That's a nasty solution. Frankly, I would recommend that you trust the engineers to be able to figure things out. If you have 300 variables, sooner or later there's a chance that one of the engineers is going to write their own get_foo method and overwrite your accessor, thus causing strange problems that are annoying to debug.

    Further, the first time that AUTOLOAD is triggered, this implies that AUTOLOAD is going to be in your module's namespace. This means that they already know how to call functions and methods there. If you stick the AUTOLOAD in the namespace of the calling programs, you open up a an entirely different can of worms.

    It seems to me that if you go down this treacherous road, you want to create an import method that simply adds generated subs to the calling namespace, but I still wouldn't do that. It's just too messy.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.