Bloodnok has asked for the wisdom of the Perl Monks concerning the following question:

Time again to prostrate myself before you highly-esteemed fellow monks concerning a problem I've encountered with Object::InsideOut ... and more specifically, the type validation of fields therein.

If a field type validation is declared as follows:

sub validate_max_size { my $val = shift; ! defined $val || $val > 0; } . . # Then later ... . my @data :Field :Type( \&package::validate_max_size );
the following error is seen when OIO calls the sub with a value of undef (aka the constructor is called with type => undef) ...
# Failed test 'Valid max_size: undef' # at (eval 4) line 43. # died: OIO::Code error: Problem with type check routine for initializ +er 'max_size' for class 'IBM::Common::Collection # Error: Argument "undef" isn't numeric in numeric gt (>) # Package: main # File: t/IBM-Common-Collection.t # Line: 68
The problem is that no error unsues from running similar code as a one-liner e.g.
perl -Mstrict -we 'sub f {my $v = shift; ! defined $v || $v > 0}; f(un +def)'

Here's the rub: Has anybody else encountered (and hopefully, overcome) this problem ??

TIA ,

Update: Thanx, once again to ikegami who noticed a disparity between the use of or and ||. Having now tried all combinations of the 2 in the 2 circumstances, exasperatingly, the principle problem remains.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re: Object::InsideOut field type validation
by ikegami (Patriarch) on Feb 05, 2009 at 19:25 UTC

    Note that you used
    ! defined $val || $val > 0
    in one place and
    ! defined $v or $v > 0
    in the other.

    Argument "undef" isn't numeric in numeric gt (>)

    That's not a Perl message. Perl 5.6 to 5.10 all return

    Use of uninitialized value in numeric gt (>)

    Update: LOL! As oshalla points out, this is the warning you get for the string 'undef', as opposed to an undefined value.

      TFT ikegami, I knew it would be something simple.

      Having said that, the problem can be removed by including no warnings; in the code ...

      sub validate_max_size { my $val = shift; no warnings; ! defined $val || $val > 0; }
      gets rid of the problem ... or at least allows a different problem to surface in my test harness ;-)

      A user level that continues to overstate my experience :-))

        I don't get it :-( I tried:

        use strict ; use warnings ; try() ; # none: 1 try(undef) ; # undef: 1 try(-1) ; # -1: 0 try(0) ; # 0: 0 try(1) ; # 1: 1 sub try { my ($s) = @_ ; my $r = validate_max_size(@_) ; if (@_ == 0) { $s = "none" ; } elsif (!defined($s)) { $s = "undef" ; } ; printf "%8s: %d\n", $s, $r+0 ; } ; sub validate_max_size { my $val = shift; ! defined $val || $val > 0; }
        on perl 5.10.0, and got no warnings...

        I don't know if there's an ointment for bracket-alergy as extreme as this... I would write:

        !defined($val) || ($val > 0) ;
        'cos I'm comfortable with unaries binding tighter than binaries... but the effect is the same as what you writ.

        Wait a tick... this gives the error you mentioned:

        try('undef') ; # Argument "undef" isn't numeric in numeric gt (>) . +..
        could it be that somebody, somewhere, is mapping undef to 'undef' ?