use strict; use warnings; package Music; use Carp; our $AUTOLOAD; { my %_attrs = ( _name => undef, _artist => undef, _publisher => undef, _ISBN => undef, _tracks => undef, _rating => undef, _room => undef, _shelf => undef, ); sub _accessible { exists $_attrs{$_[1]}} } { my $_count = 0; # These are class methods. sub get_count { $_count } # This method is global to the program my $_incr_count = sub { ++$_count }; sub new # Constructor called "new" { my ($class) = @_; # Gets class passed to constructor $_incr_count->(); bless { # Creates a blessed reference... _name => $_[1], # _artist => $_[2], # _publisher => $_[3], # _ISBN => $_[4], # _tracks => $_[5], # Simply assigns hash key variables to _room => $_[6], # the arguments passed to the constructor. _shelf => $_[7], # _rating => $_[8], # _read_count => 0, }, $class; # in this class } } sub AUTOLOAD { my ($self) = @_; $AUTOLOAD =~ /.*::get(_\w+)/ or croak "No such method: $AUTOLOAD"; $self->_accessible($1) or croak "No such attribute: $1"; $self->{_read_count}++; return $self->{$1} } sub get_location {($_[0]->{_room}, $_[0]->{_shelf}) } sub set_location { my ($self, $shelf, $room) = @_; $self->{_room} = $room; $self->{_shelf} = $shelf; } sub set_rating { my ($self, $rating) = @_; $self->{_rating} = $rating; } 1;