in reply to Re: proper Sub::Contract use
in thread proper Sub::Contract use

Thanks for the comments. $_ is corrected (my mistake) and (1) was there because I wanted to write about this line, as it should print "Scalar expected, reference found on position $index." when called normally, and "Scalar expected, reference found on position $index in row $row." when called from &is_array_of_array_of_scalar(). Just problem of finding context.

Structures will grow and AoA will be part of another bigger one, so simple grep won't be enough.

I tried exceptions mechanism, but I think It has no use here - there is no way to handle exceptions outside validation functions. I decided just to use warn "in cascades", so I get info message like this:

V::is_array_of_scalar: Scalar expected, HASH reference found on positi +on 2. Input: $VAR1 = [ 'abc', 'def', { 'some' => 'any' }, 'x', 'something' ]; V::is_array_of_array_of_scalar: Wrong value(s) in row 1. at scripts/li +b/V.pm line 102. input argument of [FDFr::compareArr] for key '-elem2' fails its contra +ct constraint at (eval 12) line 31

but it will be pretty unreadable as V module will grow. So finally your hint about passing additional argument seems to be the best solution. If the function is called as top-level validator (no possibility to pass additional arguments), it will just print its error message, but if is called by other validation function, passing arguments is possible to pass any arguments and change messages. Thanks for ideas, I'm going back to work :)

Replies are listed 'Best First'.
Re^3: proper Sub::Contract use
by JadeNB (Chaplain) on Sep 03, 2008 at 19:36 UTC
    I tried exceptions mechanism, but I think It has no use here - there is no way to handle exceptions outside validation functions. I decided just to use warn "in cascades", so I get info message like this:
    I think that your idea of warning in cascades is a good solution to the problem, so I don't want to discourage it, but I am confused by your qualification. What do you mean when you say that there is no way to handle exceptions outside validation functions? Unless I misunderstand what you're doing, all you have to do is wrap every validation in an eval {}, and check afterwards whether you caught an exception—which is not much more complicated than checking for truth or falsity anyway.