| 1 | package KinoSearch1::Util::VerifyArgs; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Scalar::Util qw( blessed ); | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | use base qw( Exporter ); | ||
| 9 | |||
| 10 | our @EXPORT_OK = qw( verify_args kerror a_isa_b ); | ||
| 11 | |||
| 12 | my $kerror; | ||
| 13 | |||
| 14 | sub kerror {$kerror} | ||
| 15 | |||
| 16 | # Verify that named parameters exist in a defaults hash. | ||
| 17 | sub verify_args { | ||
| 18 | my $defaults = shift; # leave the rest of @_ intact | ||
| 19 | |||
| 20 | # verify that args came in pairs | ||
| 21 | if ( @_ % 2 ) { | ||
| 22 | my ( $package, $filename, $line ) = caller(1); | ||
| 23 | $kerror | ||
| 24 | = "Parameter error: odd number of args at $filename line $line\n"; | ||
| 25 | return 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | # verify keys, ignore values | ||
| 29 | while (@_) { | ||
| 30 | my ( $var, undef ) = ( shift, shift ); | ||
| 31 | next if exists $defaults->{$var}; | ||
| 32 | my ( $package, $filename, $line ) = caller(1); | ||
| 33 | $kerror = "Invalid parameter: '$var' at $filename line $line\n"; | ||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | |||
| 37 | return 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | =begin comment | ||
| 41 | |||
| 42 | a_isa_b serves the same purpose as the isa method from UNIVERSAL, only it is | ||
| 43 | called as a function rather than a method. | ||
| 44 | |||
| 45 | # safer than $foo->isa($class), which crashes if $foo isn't blessed | ||
| 46 | my $confirm = a_isa_b( $foo, $class ); | ||
| 47 | |||
| 48 | =end comment | ||
| 49 | =cut | ||
| 50 | |||
| 51 | sub a_isa_b { | ||
| 52 | my ( $item, $class_name ) = @_; | ||
| 53 | return 0 unless blessed($item); | ||
| 54 | return $item->isa($class_name); | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | __END__ |