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

Replies are listed 'Best First'.
Re: Am I asking for trouble with my own AUTOLOAD sub?
by dws (Chancellor) on Feb 02, 2002 at 02:45 UTC
    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?

    A bit of paranoia can be healthy.

    One of the more robust and interesting AUTOLOADs I've seen is in CGI.pm (actually, in CGI::_compile, which CGI::AUTOLOAD delegates to). I recommend it for careful study. It shows a few edge cases that many examples don't cover.

    I see that your AUTOLOAD doesn't actually introduce any new symbols in a package's namespace. Rather, it's just a backstop that make indirect subroutine calls. Is this what you intended?

      Rather, it's just a backstop that make indirect subroutine calls. Is this what you intended?

      Yes, I originally wrote the AUTOLOAD for Address.pm and thought, "Hey, I could stick this in a base class and use it to supply automatic methods for other classes as well." Extending Laziness with OO.




      HTH,
      Charles K. Clarkson