Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

statico (5018)

AOL IM: eisforian (Add Buddy, Send Message)


Co-author of Perl Testing: A Developer's Notebook []

Journal of statico (5018)

Friday February 04, 2005
08:51 AM

class::contract woes

[ #23017 ]

My partner and I were up to 3 AM working on our homework. What killed us at the end was the fact that with all of our Class::Contract modules, preconditions on attributes do not work.

My code appears as follows:

    # x coordinate of Tile on Graph
    # assumptions: +1 => EAST
    attr 'x';
        pre { is_int($_[0]) };
            failmsg "x attribute must be an integer";

Fairly straightforward, right? Before the x attribute is set, make sure that it's an integer (via Data::Types::is_int). The problem is that when this precondition is called, the arguments are not available.

This morning I fired up the debugger and found that the precondition is actually called twice: once with the argument list, and then once to run the closure. That doesn't make sense.

The docs and tests neither document or test this functionality, yet everything in the instructions seems to point to it. Yes, you can use the value() function to retrieve the value of the accessor, but that doesn't any good in a precondition where the value hasn't been set yet.


The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.