Basically I'm trying to get an understanding on what people use AUTOLOAD for in general, and what is considered good practice (holy war to follow). There are a lot of *interesting* things that can be done with AUTOLOAD, but as i get deeper, i keep finding conflicting opinions / reasons as to AUTOLOAD or not to AUTOLOAD.
It's a nice timesaver to use it to handle simple accessor / mutators without having to write them explicitly:
But if you're inheriting from this class, you definitely need to know too much detail about the implementation, particularly that its using AUTOLOAD for some accessor / mutators.package some_package; ... { my %simple_data = ( 'name' => 1, 'age' => 1, ... } sub AUTOLOAD { my $self = shift; (my $method) = $AUTOLOAD) =~ s/.*:://; return if $method eq 'DESTROY'; if ( exists $simple_data{$method} ) { if ( defined $_[0] ) { $self->{$method} = $_[0]; } return $self->{$method}; } } }
As was sort of talked about in my other thread, perhaps a better way to do this is along the lines of:
This way, these accessor / mutators are in the symbol table, and will be looked up faster, will be found by can(), etc. This avoids the inheritance problem from the other post too.package some_package; ... { BEGIN { my @simple_data = qw(name age ...); no strict 'refs'; foreach my $data (@simple_data) { *{$data} = sub { my $self = shift; my ($value) = @_; if ( defined $value ) { $self->{$data} = $value; } return $self->{$data}; } } use strict; }
Perhaps part of my problem is that a lot of my ideas come from Damian Conways 'Object Oriented Perl' (Manning 2000), which is perhaps outdated.
Or maybe AUTOLOAD can be used for a lot of neat tricks in little scripts, but becomes quite annoying for large software projects.
In reply to AUTOLOAD - the good, the bad, and the ugly by shemp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |