#!/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;
####
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;
##
##
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;