http://www.perlmonks.org?node_id=412698


in reply to Use method/function signatures with Perl

This is kind of cool, but I've been severely bitten by source filters in the past, and continue to have a number of personal stylistic issues with using them. What I'd like to see (and if someone knows of a CPAN module that does this, I'd be grateful) is something that would allow subs to be written thusly:

use Verifier; my $verify = new Verifier (on_failure => sub { die(join (',',@_)) }) sub foobar { ## The '-' in front of hashref means $bar is an optional paramater my ($foo, $bar) = $verify->type ( \@_, qw/scalar -hashref/); print "Using $foo:\n"; return $foo unless %$bar; for (keys %$bar) { print "$_ => $$bar{$_}\n" } return $$bar{'result'}; }

That is, I'd really like to see verification of parameter types handled by a verifier object rather than by a source filter. Yes, I know it's not quite the same thing; but by eval'ing the type verifier call, one could achieve the same basic functionality.

Update: It turns out, thanks to a mention by dragonchild and xdg, that Params::Validate does just the above. An example of the above code, but using that CPAN module, can be found a bit further down in this discussion.

radiantmatrix
require General::Disclaimer;
s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}

Replies are listed 'Best First'.
Re^2: Use method/function signatures with Perl
by dragonchild (Archbishop) on Dec 06, 2004 at 17:10 UTC
    What's wrong with how Params::Validate does things?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Aha. I knew someone must have had the same general idea before! Thank you for that link. I only have a tiny gripe with it vs. my ideal verifier, which is that Params::Validate only validates, where as what I wrote above would imply validation and returning of the validated parameters (rolling two operations into one).

      I'm certainly not about to reinvent a wheel over something so minor, though. The module you recommended will work nicely. Thanks again!

      Update: this is what I get for merely scanning the POD before posting. As xdg points out, Params::Validate does everything I have wished for:

      use Params::Validate; sub foobar { my ($foo, $bar) = validate_pos ( @_, {type = SCALAR, regex=>/.+/}, {type = HASH_REF} ); print "Using $foo:\n"; return $foo unless %$bar; for (keys %$bar) { print "$_ => $$bar{$_}\n" } return $$bar{'result'}; }

      is equivalent to my earlier code.

      radiantmatrix
      require General::Disclaimer;
      s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}

        I think if you read the POD closely, you'll see Params::Validate does what you want. E.g. (from the POD,

        my @p = validate( @_, 1, { default => 99 } );

        You can also set a global on_fail callback to die however you want.

        -xdg

        Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.