null
null

Reputation: 928

Perl dynamically creating methods

I've researched (google) for without success, so I turn to S.O. for help.

I seem to remember a way of coding a perl package so that if any missing methods that are called, a helper function will perform the action.

All the 'missing' methods have the same look and feel. They just need call another library with the passed in arguments, plus the name of the 'missing' method.

The class will have some concrete methods, eg. constructor and destructors.

Many thanks.

Upvotes: 1

Views: 426

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272417

I think you're looking for autoloading

Normally, you can't call a subroutine that isn't defined. However, if there is a subroutine named AUTOLOAD in the undefined subroutine's package (or in the case of an object method, in the package of any of the object's base classes), then the AUTOLOAD subroutine is called with the same arguments that would have been passed to the original subroutine. You can define the AUTOLOAD subroutine to return values just like a regular subroutine, or you can make it define the routine that didn't exist and then call that as if it'd been there all along.

The function/method name called is available thus:

my $method= our $AUTOLOAD;

Upvotes: 3

Related Questions