package Bill; use strict; use warnings; our ( $AUTOLOAD ); sub AUTOLOAD { my ( $self, $newval ) = @_; no strict q{refs}; if( $AUTOLOAD =~ /.*::get_(.+)/ ) { my $attr = $1; # Class Attribute name. *{$AUTOLOAD} = sub { $_[0]->( q{get}, $attr ) }; #cache return $self->( q{get}, $attr ); } elsif ( $AUTOLOAD =~ /.*::set_(.+)/ ) { my $attr = $1; *{$AUTOLOAD} = sub { $_[0]->( q{set}, $attr, $_[1] ) }; #cache return $self->( q{set}, $attr, $newval ); } } sub new { my ( $class, $args ) = @_; my %attribs; # Hold and ecapsulate all object attribs. my @protected_attribs = qw( userid access_level ); # This means that rather than being able to get at object # attribs like this... # $self->{obj_attrib} # you have to do this... # $self->( 'get', 'obj_attrib' ); # $self->( 'set', 'obj_attrib', 'new_value' ); # OR through use of the AUTOLOAD routine... # $self->get_obj_attrib; # $self->set_obj_attrib( 'new_value' ); my $self = sub { my ( $cmd, $attr, $newval ) = @_; if ( $cmd eq q{set} ) { unless ( scalar grep { $attr eq $_ } @protected_attribs ) { $attribs{$attr} = $newval } } return $attribs{$attr}; }; $self = bless $self, $class; #----------------------------------------- # Do any initialization that is needed for the objects data $attribs{dbh} = $args->{dbh}; return wantarray ? ( 1, $self ) : $self; } # END new