I started learning this new cool feature, but after few hours of reading and trying I don't know how to program more complicated check functions. Is it possible to use one check function as part of another function? Let's assume I have function, which expects two references as parameters: ref to simple array of scalars and ref to AoA of scalars. My validation module looks like this:
package V; use Carp; sub is_array_of_scalar { my $arg = shift; if (!defined $arg) { warn "Undefined value! Expected reference to an array of scala +rs!"; return 0 } if(ref $arg ne "ARRAY") { warn "Expected ARRAY reference, found " . ref($arg) . "."; return 0; } for my $index(0..$#$arg) { if(!defined($arg[$index])) { warn "Undefined value found on position $index."; return 0 } if(ref($arg[$index]) ne "") { warn "Scalar expected, reference found on position $index. +"; # (1) return 0 } } return 1; } sub is_array_of_array_of_scalar { my $arg = shift; if (!defined $arg) { warn "Undefined value! Expected reference to an array of scala +rs!"; return 0 } if(ref $arg ne "ARRAY") { warn "Expected ARRAY reference, found " . ref($arg) . "."; return 0; } for my $index(0..$#$arg) { if(!is_array_of_scalar($arg[$index])) { warn "Wrong value(s) in row $index."; return 0 } } return 1; } "true value at the very end";

And here is the problem: Is it possible to say precisely, i.e. that some wrong value is in row 5, column 3? Above code won't do it. Function is_array_of_scalar doesn't know, which column it is checking. Is the Sub::Contract module proper to perform such checks or rather to do simpler (and not nested) ones only?

Update: $_ corrected in loop, thanks JadeNB


In reply to proper Sub::Contract use by grizzley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.