I like this module, and Ensure is an appropriate name IMO, but Loop::Invariant describes more clearly what the programmer intends to happen. Contract is a close third, while I find Assert unexpected in the sense that the program dosen't die when an assertion fails.
But I have some problems with running your module :
- It dosen't run under Perl 5.5.3 (ActiveState Perl build 517)
- Under Perl 5.6.0 (IndigoPerl), I get Can't use string ("") as a subroutine ref while "strict refs" in use at Ensure.pm line 56. as an error in the Ensure::Scalar::FETCH sub. I think this comes from a conflict in execution order as follows :
- Ensure::using is called to tie the scalar, and sets up the Ensure::Scalar::FETCH handler.
- Ensure::ensure is called, and itself calls tied($$_), which in turn results in Ensure::Scalar::FETCH being called.
- Ensure::Scalar::FETCH is called and runs into the undef, as the handlers have not yet been set up (from within Ensure::ensure).
The quick fix to me was a small change to the Ensure::Scalar module as follows :
sub TIESCALAR {
my ($class, $val) = @_;
my $ref = [ $val, "setup", "setup" ];
bless $ref, $class;
return $ref;
}
sub FETCH {
my $self = shift;
my $val = $self->[0];
if (ref($self->[1]) eq 'CODE') {
die "[Ensure]\n" unless $self->[1]->();
};
return $val;
}
This fix would have to be repeated for the other modules (or maybe isn't necessary at all with Perl 5.6.1), and I'm not sure if my fix is really a fix and dosen't introduce more errors, as this is the first time I've dabbled with
tie :-)