I'm not sure there is practical utility. The first thing is that closures provide "hard encapsulation", where the only way to the internals of an object is through the interface. This comes at a small CPU hit, naturally.
My immediate thought is for flyweight objects that take more than one scalar's worth of info. Instead of using class-level parallel arrays to keep the info, I was thinking that, maybe, closures would be more memory-efficient. (This is over arrays or hashes.)
Now, yes, I know that optimizing for memory isn't necessarily a good thing, especially this early in the game. But, I just want to keep my options open and see what the comparison is.
An example of what I'm talking about would be:
sub new {
my $class = shift;
return undef if ref $class;
my ($first, $second) = @_;
my $self = sub {
my $var = shift;
my ($mode, $newval) = @_;
if ($var == 0) {
return $mode ? $first = $newval : $first;
} elsif ($var == 1) {
return $mode ? $second = $newval : $second;
}
return undef;
};
bless $self, $class;
return $self;
}
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. |