http://qs321.pair.com?node_id=142852

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


I wrote MyBaseClass.pm to avoid having to write methods for each hash item in the derived classes. Amazingly, it works as I intended. In this example, Address.pm inherits the new method from MyBaseClass.pm and the state method is handled in the AUTOLOAD sub in MyBaseClass.pm.

This allows me to call state as a method <nobr>(print $c->state)</nobr> and avoid accessing the object's hash directly with <nobr>print $c->{address}{state}</nobr>. Further it will return undef if the method doesn't exist, allowing me to test for an additional address line, for example. I'm wondering if there is something I'm not anticipating. Is there a problem with this approach that I don't see or am I just paranoid?

testing.pl

#!/usr/bin/perl use strict; use warnings; use Address; my $c = new Address; $c->address( [ '137 PR 1101', 'Sun Valley MHP - FM 205', 'FooHaHa, TX 76401' ] ); print $c->state;

MyBaseClass.pm

package MyBaseClass; use strict; use warnings; use vars '$AUTOLOAD'; # or: our '$AUTOLOAD'; sub new { my $this = shift; my $class = ref( $this ) || $this; return bless {}, $class; } sub AUTOLOAD { my $self = shift; if ( $AUTOLOAD =~ /::(.*)$/ ) { my $method = $1; if ( $method ne 'DESTROY') { return $self->{address}{$method} if exists $self->{address +}{$method}; return undef; } } } 1;

Address.pm

package Address; use strict; use warnings; use base 'MyBaseClass'; sub address { my $self = shift; return $self->{address} unless @_; #rip apart address @{ $self->{address} }{ qw(number street) } = split ' ', ( shift @ +{ $_[0] } ), 2; my $line = pop @{ $_[0] }; @{ $self->{address} }{ qw(city state zip) } = $line =~ /([^,]),\s* +(\S+)\s*(\d+)/; $self->{address}{additional} = $_[0] if @{ $_[0] }; return $self->{address}; } 1;



HTH,
Charles K. Clarkson