package PRODUCT; use Carp; use strict; use vars qw($AUTOLOAD); my %fields = ( name => undef, price => undef, description => undef, ); sub new { my $product = shift; my $class = ref($product) || $product; my $self = { _permitted => \%fields, %fields, }; bless $self, $class; return $self; } sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; #strip fully-qualified portions unless (exists $self ->{_permitted} ->{$name} ) { croak "Can't access `$name' field in class $type"; } if (@_) { return $self -> {$name} = shift; }else{ return $self-> {$name}; } } #### ### Pack s the current field into a comma delimited record ### and returns it sub pack { my ( $self ) = @_; my $record = join(':', $self->{name} , $self->{price}, $self->{description}); return $record; }