
#--------------------------
# bank accounts redone:

package account;
sub makeaccount
{  my $balance = $_[0];

   my $deposit = sub { $balance += $_[0]; $balance; };

   my $withdraw = sub { if ($_[0]<=$balance) 
			     { $balance-=$_[0]; $balance; } 
                        else { print "insufficient funds\n"; }
                      };
   my $dispatch =
   { 
     withdraw => $withdraw,
     deposit => $deposit,
     extend => sub {  my $sc = shift;
		      $sc->($balance);
                   }
   };
   bless $dispatch, 'account';
   $dispatch;
}


sub subaccount
{
    $balancep = $_[0];
    
}


$account0 = makeaccount(100);
$account1 = makeaccount(200);
print $account0->{deposit}->(50);  # deposit $50 into account0
$account1->{withdraw}->(100); # withdraw $100 from account1
print "\n";

#----
package savingsaccount;
sub makeaccount
{
    my $newaccount = account::makeaccount($_[0]);
    my $interestrate = $_[1];
    $newaccount->{interestrate} = $interestrate;  
    bless $newaccount, 'savingsaccount';
    $newaccount;
}

sub accept
{
    my ($acc,$member) = @_;
    $acc->{$member};        
}

$account2 = savingsaccount::makeaccount(200,0.02);
print $account2->{interestrate}, "\n";
print "\n";

# perl interpretive shell:
#while (<STDIN>) { eval; print $@; }
