# Bank account objects in perl (as closures)

sub newaccount
{
    my $b = $_[0];
    my $withdraw = sub { $b = $b - $_[0] };
    my $deposit = sub { $b = $b + $_[0] };
    my $inquiry = sub { $b };
    
    my $interface = sub
    {
	my $request = $_[0];
	if ($request eq "withdraw") {return $withdraw}
	if ($request eq "deposit")  {return $deposit}
	if ($request == "inquiry")  {return $inquiry->() }
	else { die "error"; }
    };

    $interface;
}

$myaccount = newaccount(100);
$youraccount = newaccount(200);

$youraccount->(deposit)->(50);
$myaccount->(withdraw)->(100);

print $myaccount->(inquiry), "\n";
print $youraccount->(inquiry), "\n";


 

