package Rekening; # class for storing information about an account and doing a little quick credit transaction use warnings; use strict; use Carp; sub new { my $class = shift; my $self = { ACCOUNT => undef, NAME => undef, BALANCE => 0, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure,$class); return $closure; } # public accessors sub account { &{ $_[0] }("ACCOUNT", @_[1 .. $#_]) } sub name { &{ $_[0] }("NAME", @_[1 .. $#_]) } sub balance { &{ $_[0] }("BALANCE", @_[1 .. $#_]) } # protected method printMe sub printMe { caller(0)->isa(__PACKAGE__) || confess "cannot call protected method\n"; my $self = shift; print $self->account . " " . $self->name . " " . $self->balance . "\n"; } # private method add my $add = sub { my $self = shift; my $amount = shift; $self->balance($self->balance+$amount); }; # private method padd sub padd { my $self = shift; my $amount = shift; caller(0) eq __PACKAGE__ || confess "cannot call private method"; $self->balance($self->balance+$amount); } # public method credit sub credit { my $self = shift; my $amount = shift; $self->padd($amount); } 1;