File Coverage

blib/lib/Term/Choose/ValidateOptions.pm
Criterion Covered Total %
statement 31 36 86.1
branch 20 34 58.8
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 57 77 74.0


line stmt bran cond sub pod time code
1             package Term::Choose::ValidateOptions;
2              
3 3     3   21 use warnings;
  3         8  
  3         806  
4 3     3   22 use strict;
  3         32  
  3         84  
5 3     3   40 use 5.10.1;
  3         13  
6              
7             our $VERSION = '1.782';
8              
9 3     3   23 use Exporter qw( import );
  3         18  
  3         199  
10              
11             our @EXPORT_OK = qw( validate_options );
12              
13 3     3   17 use Carp qw( croak );
  3         5  
  3         1503  
14              
15              
16             sub validate_options {
17 111     111 0 304 my ( $valid, $opt, $caller ) = @_;
18 111 50       268 return if ! defined $opt; #
19 111 50       296 if ( ! defined $caller ) { #
20 0         0 $caller = '';
21             };
22 111         427 for my $key ( keys %$opt ) {
23 150 50       461 if ( ! exists $valid->{$key} ) {
24 0         0 croak "$caller: '$key' is not a valid option name";
25             }
26 150 100       407 next if ! defined $opt->{$key};
27 113 100       2337 if ( $valid->{$key} =~ /^Array/ ) {
    50          
    50          
    100          
    50          
28 16 50       54 croak "$caller: option '$key' => the passed value has to be an ARRAY reference." if ref $opt->{$key} ne 'ARRAY';
29 16 100       55 if ( $valid->{$key} eq 'Array_Int_Idx' ) {
    50          
30 8         18 for ( @{$opt->{$key}} ) {
  8         21  
31 19 50       44 defined or croak "$caller: option '$key' => undefined array element";
32 19 50       92 /^[0-9]+\z/ or croak "$caller: option '$key' => $_ is an invalid array element";
33             }
34             }
35             elsif ( $valid->{$key} eq 'Array_Int' ) {
36 8         16 for ( @{$opt->{$key}} ) {
  8         24  
37 9 50       22 if ( defined ) {
38 9 50       59 /^[0-9]+\z/ or croak "$caller: option '$key' => $_ is an invalid array element";
39             }
40             }
41             }
42             }
43             elsif ( $valid->{$key} =~ /^Regexp/ ) {
44 0 0         croak "$caller: option '$key' => the passed value has to be a regex quoted with the 'qr' operator." if ref $opt->{$key} ne 'Regexp';
45             }
46             elsif ( ref $opt->{$key} ) {
47 0           croak "$caller: option '$key' => a reference is not a valid value.";
48             }
49             elsif ( $valid->{$key} eq 'Str' ) {
50             }
51             elsif ( $opt->{$key} !~ m/^$valid->{$key}\z/x ) {
52 0           croak "$caller: option '$key' => '$opt->{$key}' is not a valid value.";
53             }
54             }
55             }
56              
57              
58              
59              
60              
61              
62             1;