Before anyone admonishes me for reinventing the wheel, let me say that I searched CPAN and found nothing that did what I need this module to do.

What I need is a way to, possibly recursively, check structures for type -- for instance, one should be able to check if it matches certain regexen, for strings, or isa a certain class, or isa a certain type, or contains only elements or keys of certain types, or returns true when given as the argument to a supplied coderef, etc. So I planned it out and coded it. Originally I was going to use Quantum::Superpositions rather than grep here, but it produced copious amounts of warnings with -w, so I used grep instead, which didn't.

The problem I'm having is that when given certain pieces of data that should not match the template given, it returns true anyway.

The relevant section of code:

sub typeck { my ($what, $type) = @_; if (!UNIVERSAL::isa($type, 'ARRAY')) { croak 'Type not an array re +ference' } not grep { !$_ } ( map { grep { $_ } ( (index($_, '/') == 1 and rindex($_, '/') == (length($_) - 1 +) and do { my $rex = substr($_, 1, length($_) - 1); (ref($what) eq '') ? ($what =~ $rex) : ((Denter $what) =~ $ +rex); }), (UNIVERSAL::isa($_, 'ARRAY') && do { my $type = $_; not grep {!$_} (map { typeck ($_, $type) } ((UNIVERSAL::isa($what, 'ARRAY' +)) ? @$what : values(%$what))); }), (UNIVERSAL::isa($_, 'HASH') && do { my $type = $_; not grep {!$_} (map { exists ($type->{$_}) } (keys %$what)); }), (UNIVERSAL::isa($_, 'CODE') && $_->($what)), (UNIVERSAL::isa($what, $_))) } (@$type)); }
The data and type template, Data::Dentered:
# -- data --
@
    10
    345
    badvalue
    49120
# -- type --
@
    ARRAY
    @
        /^0-9+$/
When run, &typeck seems to be skipping entirely over the template, returning 1 always. I looked through the code and found no errors, and the Perl debugger was of no help, so I turn to the monks for help.

Can anyone figure out what is going on here?


In reply to Type checker not checking out by premchai21

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.