sub new { my $class = shift; my $name = shift; my $self = { NAME => $name, CREDITAMOUNT => Math::BigInt->new(0), DEBETAMOUNT => Math::BigInt->new(0), CREDITACCOUNTS => [], DEBETACCOUNTS => [], }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless( $closure, $class ); return $closure; } #### # public to add accounts to the creditaccounts sub addCreditAccount { my $closure = shift; my $account = shift; my $accountsref = &{$closure}( "CREDITACCOUNTS" ); push @{ $accountsref }, $account; } #### # public getBalansCreditAmount to get the amount of all accounts sub getBalansCreditAmount { my $closure = shift; my $totalCreditAmount = $closure->getCreditAmount(); for my $balans ( $closure->getCreditAccounts() ) { $totalCreditAmount += $balans->getBalansCreditAmount(); } return $totalCreditAmount; } #### # private getCreditAccounts sub getCreditAccounts { caller(0) eq __PACKAGE__ || confess "getCreditAccounts is private"; my $closure = shift; my @accounts = @{ &{$closure}( "CREDITACCOUNTS" ) }; return @accounts; }