in reply to Passing a value from a module

You could just arrange your interface to return error messages as well as the 'cleaned' value.

sub numbers { my ($class,$number) = @_; if ( /(\d+)/ ) { return ( $1, { msg => 'ok' } ); } else { return ( undef, { msg => 'numbers, please' } ); } } ... my ($age,$stuff) = Validate->numbers( ... ); unless ( $age ) { push @errors, $stuff->{msg}; }

Also, I know this is just example code, but your regex to validate numbers isn't very solid. You'd be much better off with Regexp::Common.

qq

Replies are listed 'Best First'.
Re: Re: Passing a value from a module
by bradcathey (Prior) on Apr 15, 2004 at 13:42 UTC
    qq thanks for your reply. It worked wonderfully. In fact, I adapted it to have better messages:
    sub numbers { my ($class, $value, $len) = @_; if (!$value) { return (undef, { msg => 'cannot be blank.' }); } elsif (length($value) > $len ) { return (undef, { msg => 'has too many characters.' }); } elsif ($value !~ /^([0-9 \.-]*)$/) { return (undef, { msg => 'can only use numbers.' }); } else { return ("$1", { msg => '' }); } }

    —Brad
    "A little yeast leavens the whole dough."

      Careful about your tests, though. 0 isn't true, so you'll get the "cannot be blank" message. And /^([0-9 \.-]*)$/ would match '.-.. ', '--0.0.3....' , ' - ', etc.

      qq