#!/bin/perl

# What is the role of static (lexical) scoping in oop?

sub newaccount
{
   local $balance = $_[0];

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

   my $chargefee = sub { $balance -= 3; };
   my $withdraw = sub 
		  { $balance = $balance - $_[0]; &$chargefee(); };
  
   # return interface function:
   sub
   {
     local $method = $_[0]; # requested method
     if ($method eq withdraw) { return $withdraw; }
     if ($method eq deposit)  { return $deposit; }
     if ($method eq inquiry)  { return &$inquiry(); }
       else { die "error"; }
   }
}

my $myaccount = &newaccount(500);
my $youraccount = &newaccount(800);

local $balance = 100;   # will use this balance instead!

$myaccount->(withdraw)->(30);
&{&$youraccount(deposit)}(70);   # alternate syntax

print "my balance is ", $myaccount->(inquiry), "\n";
print "your balance is ", $youraccount->(inquiry), "\n";


