... tighter encapsulation ...
I doubt any of this will be new to you, but just to clarify my thoughts for myself, I suppose what I really had in mind was actually two separate and disparate effects:
-
If a 'private' closure is not further encapsulated in a use-d module, pesky order-of-evaluation effects can manifest:
>perl -wMstrict -le
"print 'before: ', defined S() ? S() : 'undefined';
;;
{
my $x = 42;
;;
sub S { return $x; }
}
;;
print 'after: ', defined S() ? S() : 'undefined';
"
before: undefined
after: 42
-
If such a closure is encapsulated in a used-d module, the order-of-evaluation problems go away (assuming you're not doing any fancy-schmancy CTFE in the module, I think this is reliably true), but the 'tighter' aspect, strictly speaking, then kicks in: a state variable really is private to the function in which it's defined, but 'private' closures allow the sharing of access to variables among several functions — in itself, a highly useful property in certain circumstances!
>perl -wMstrict -le
"print M::get_x();
;;
use M;
;;
print M::get_x();
"
42
42
Where M.pm is:
package M;
{ my $x = 42;
sub get_x { return $x; }
sub set_x { return $x = $_[0]; }
}
my $y = 1729;
sub cannot_access_x { return $y; }
1;
Oh, and:
... consistently 2% faster ...
I'm not sure I really believe in a 2% performance difference reported by Benchmark, but as long as it's you...!