And you should know, Abigail!!!
Actually, as Abigail just did (go quickly and grab Closures-1.2 in http://cpan.org/modules/by-authors/id/ABIGAIL/), you can construct a whole object system on top the closures (it's been done some dozen times in different ways in Scheme, where basically everything is a closure).
A simpler example, consider this code:
use strict;
use warnings;
sub Person
{
my %self = ();
my @data = ( qw/name age/ );
my %mem = ();
sub
{
for my $sub ( @data )
{
$self{$sub} = sub {
$mem{$sub} = $_[0] if @_; return $mem{$sub} }
}
return \&{$self{$_[0]}} if defined $self{$_[0]};
die "Method '$_[0]' unknown to class Person";
}
}
We create a closure which exposes the interface of the class and holds both the methods (in %self) and data (in %mem, from memory). Please, note that it's impossible to read or write to the values held in %mem or %self (ie, modify the state of the object or modify its interface) directly, only through the interface exposed, which is very hygienic in itself.
Using this is easy (if somewhat not very idiomatic):
# creating object
my $john = Person;
# initializing values
$john->('name')->('John');
$john->('age')->(23);
# working with object
print "I'm ",
$john->('name')->(),
" and I am ",
$john->('age')->(),
" years-old, but my birthday is in just 2 seconds\n";
sleep( 2 );
$john->('age')->(24);
print "Happy B-Day, ", $john->('name')->(), "!!!\n";
print "It's ", $john->('age')->(), " years-old already!!!\n";
print $john->('address')->();
__END__
Output is as expected (text between []'s is mine):
I'm John and I am 23 years-old, but my birthday is in just 2 seconds
[two seconds pass...]
Happy B-Day, John!!!
It's 24 years-old already!!!
Uncaught exception from user code:
Method 'address' unknown to class Person at closure.pl line 18
+.
main::__ANON__('address') called at closure.pl line 40
I am sure it's not too hard being able to force the dereference of the methods when you are on getter mode so instead of writing $john->('age')->() you can just say $john->('age') (as Abigail has in Closures-1.2). Probably an additional line checking the number of arguments passed to the method.
Except being the state held in the closures, this is all pure functional code; functional programming rules, yeah :)
best regards, david
--
our $Perl6 is Fantastic; |