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 :-)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|