in reply to Re^4: Modules for autogenerating accessor/mutator methods
in thread Modules for autogenerating accessor/mutator methods
Typeglob a bunch of closures. Save AUTOLOAD for a problem that needs the power. Here is an untested version.
Drawbacks? You have to write the line of code that lists the accessors. Advantages? You leave AUTOLOAD free to do something else, you don't get things that you didn't intend to AUTOLOAD being AUTOLOADed, your methods are visible to UNIVERSAL::can, missing methods don't get accidentally interpreted as accessors (a good AUTOLOAD can address this, but requires more code)...package Whatever::Makes::Sense; sub create_accessors { my $class = caller(); foreach my $accessor (@_) { no strict 'refs'; *{"$class\::$accessor"} = sub { my $self = shift; $self->{$accessor} = shift if @_; return $self->{$accessor}; }; } } # And elsewhere... Whatever::Makes::Sense::create_accessors(qw(foo bar baz));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Modules for autogenerating accessor/mutator methods
by tlm (Prior) on Jun 05, 2005 at 00:29 UTC | |
by diotalevi (Canon) on Jun 05, 2005 at 00:42 UTC | |
by tlm (Prior) on Jun 05, 2005 at 02:32 UTC | |
by diotalevi (Canon) on Jun 05, 2005 at 02:35 UTC | |
by tlm (Prior) on Jun 05, 2005 at 03:15 UTC | |
| |
|
Re^6: Modules for autogenerating accessor/mutator methods
by wazoox (Prior) on Jun 05, 2005 at 07:39 UTC | |
by tilly (Archbishop) on Jun 06, 2005 at 05:04 UTC |