in reply to Class capabilities

I'm not sure if there's a CPAN module for 'does', but your first cut will allow the foo() method to continue if at least one method is available, which may not be what you want. Also in scalar context an array ref will be returned, which will always evaluate to true, so even if no @methods pass the test, foo() will continue.

Replies are listed 'Best First'.
Re^2: Class capabilities
by Ovid (Cardinal) on Oct 11, 2005 at 21:20 UTC

    Yeah, my first cut was a bit too simple. It's now:

    sub does { my ($proto, @methods) = @_; return if ref $proto && ! blessed $proto; my @does; foreach my $method (@methods) { push @does, $proto->can($method) || (); } @does = () unless @does == @methods; return wantarray ? @does : @does ? \@does : (); }

    Thanks!

    Cheers,
    Ovid

    New address of my CGI Course.

      Kind of complicated. Why not return as soon as you fail to find any method?

      sub does { my ( $proto, @method ) = @_; return if ref $proto && !blessed $proto; my @does; push @does, $proto->can( $_ ) || return foreach @method; return wantarray ? @does : \@does; }

      That said I think diotalevi’s Params::Validate recommendation is spot on.

      Makeshifts last the longest.