#!/usr/bin/perl package Account; # new ########################################################################## sub new { my $package = shift; my $class = ref($package) || $package; my $self = {@_}; bless($self, $class); return $self; } # END: new # Generate Accessor Methods #################################################### # From "Programming Perl" 3rd Ed. p338. for my $attribute (qw(account_holder account_type)) { no strict "refs"; # So symbolic ref to typeglob works. *$attribute = sub { my $self = shift; $self->{$attribute} = shift if @_; return $self->{$attribute}; }; } # ballance ##################################################################### sub ballance { my $self = shift; return $self->{'ballance'}; } # END: ballance # deposit ###################################################################### sub deposit { my $self = shift; my $deposit = shift; $self->{'ballance'} += $deposit; return $self->{'ballance'}; } # END: deposit # withdraw ##################################################################### sub withdraw { my $self = shift; my $withdrawal = shift; $self->{'ballance'} -= $withdrawal; return $self->{'ballance'}; } # END: withdraw ################################################################################ package main; print "\nNew Account\n\n"; my $account = Account->new('ballance' => 100); $account->account_holder('Doug Miles'); $account->account_type('Free Checking'); print $account->account_holder, "\n"; print $account->account_type, "\n"; print $account->ballance, "\n"; print "\nDeposit\n\n"; $account->deposit(75); print $account->ballance, "\n"; print "\nWithdrawal\n\n"; $account->withdraw(125); print $account->ballance, "\n";