| MakeMethods::Autoload(3pm) | User Contributed Perl Documentation | MakeMethods::Autoload(3pm) |
Class::MakeMethods::Autoload - Declare generated subs with AUTOLOAD
package MyObject;
use Class::MakeMethods::Autoload 'Standard::Hash::scalar';
package main;
my $obj = bless {}, 'MyObject';
$obj->foo("Foozle");
print $obj->foo();
This package provides a generate-on-demand interface to Class::MakeMethods.
When your class uses this package, it imports an AUTOLOAD function that resolves missing methods by using Class::MakeMethods to generate and install a standard type of method.
You must specify the type of method to be generated by passing a single argument to your use Class::MakeMethods::Autoload statement, which can take any of these forms:
Here are three examples:
use Class::MakeMethods::Autoload 'Standard::Hash:scalar';
use Class::MakeMethods::Autoload 'Basic::Universal::no_op';
use Class::MakeMethods::Autoload
'::Class::MakeMethod::Composite::Global:array';
Here's a contrived example which generates scalar accessors for methods except those with a digit in their name, which are treated as globals.
use Class::MakeMethods::Autoload sub {
my $name = shift;
( $name =~ /\d/ ) ? 'Standard::Global::scalar'
: 'Standard::Hash::scalar'
};
Here's an example which provides a new() constructor, a DESTROY() method that does nothing, and a wildcard match that provides scalar accessors for all other Autoloaded methods:
use Class::MakeMethods::Autoload {
'new' => 'Standard::Hash::new',
'.*' => 'Standard::Hash::scalar',
'DESTROY' => 'Standard::Universal::no_op',
};
Here's a more sophisticated example which causes all-upper-case method names to be generated as globals, those with a leading upper-case letter to be generated as inheritable data methods, and others to be normal accessors:
use Class::MakeMethods::Autoload {
'new' => 'Standard::Hash::new',
'.*' => 'Standard::Hash::scalar',
'[A-Z].*' => 'Standard::Inheritable::scalar',
'[A-Z0-9]+' => 'Standard::Global::scalar',
'DESTROY' => 'Standard::Universal::no_op',
};
The following warnings and errors may be produced when using Class::MakeMethods::Attribute to generate methods. (Note that this list does not include run-time messages produced by calling the generated methods, or the standard messages produced by Class::MakeMethods.)
See Class::MakeMethods for general information about this distribution.
For distribution, installation, support, copyright and license information, see Class::MakeMethods::Docs::ReadMe.
| 2022-10-13 | perl v5.36.0 |