File Coverage

blib/lib/ETL/Yertl/Util.pm
Criterion Covered Total %
statement 32 34 94.1
branch 8 10 80.0
condition n/a
subroutine 7 7 100.0
pod 4 4 100.0
total 51 55 92.7


line stmt bran cond sub pod time code
1             package ETL::Yertl::Util;
2             our $VERSION = '0.036';
3             # ABSTRACT: Utility functions for Yertl modules
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod =head1 DESCRIPTION
8             #pod
9             #pod =head1 SEE ALSO
10             #pod
11             #pod =cut
12              
13 26     26   151 use ETL::Yertl;
  26         59  
  26         119  
14 26     26   803 use Exporter qw( import );
  26         48  
  26         663  
15 26     26   117 use Module::Runtime qw( use_module compose_module_name );
  26         46  
  26         112  
16              
17             our @EXPORT_OK = qw(
18             load_module pairs pairkeys firstidx
19             );
20              
21             #pod =sub load_module
22             #pod
23             #pod $class = load_module( format => $format );
24             #pod $class = load_module( protocol => $proto );
25             #pod $class = load_module( database => $db );
26             #pod
27             #pod Load a module of the given type with the given name. Throws an exception if the
28             #pod module is not found or the module cannot be loaded.
29             #pod
30             #pod This function should be used to load modules that the user requests. The error
31             #pod messages are suitable for user consumption.
32             #pod
33             #pod =cut
34              
35             sub load_module {
36 256     256 1 697 my ( $type, $name ) = @_;
37              
38 256 50       609 die "$type is required\n" unless $name;
39 256         443 my $class = eval { compose_module_name( 'ETL::Yertl::' . ucfirst $type, $name ) };
  256         1137  
40 256 100       11195 if ( $@ ) {
41 1         5 die "Unknown $type '$name'\n";
42             }
43              
44 255         396 eval {
45 255         601 use_module( $class );
46             };
47 255 100       6660 if ( $@ ) {
48 1 50       6 if ( $@ =~ /^Can't locate \S+ in \@INC/ ) {
49 1         7 die "Unknown $type '$name'\n";
50             }
51 0         0 die "Could not load $type '$name': $@";
52             }
53              
54 254         1254 return $class;
55             }
56              
57             #pod =sub pairs
58             #pod
59             #pod my @pairs = pairs @array;
60             #pod
61             #pod Return an array of arrayrefs of pairs from the given even-sized array.
62             #pod
63             #pod =cut
64              
65             # This duplicates List::Util pair, but this is not included in Perl 5.10
66             sub pairs(@) {
67 393     393 1 923 my ( @array ) = @_;
68 393         561 my @pairs;
69 393         786 while ( @array ) {
70 1153         2682 push @pairs, [ shift( @array ), shift( @array ) ];
71             }
72 393         1075 return @pairs;
73             }
74              
75             #pod =sub pairkeys
76             #pod
77             #pod my @keys = pairkeys @array;
78             #pod
79             #pod Return the first item of every pair of items in an even-sized array.
80             #pod
81             #pod =cut
82              
83             # This duplicates List::Util pairkeys, but this is not included in Perl 5.10
84             sub pairkeys(@) {
85 3     3 1 462 return map $_[$_], grep { $_ % 2 == 0 } 0..$#_;
  14         45  
86             }
87              
88             #pod =sub firstidx
89             #pod
90             #pod my $i = firstidx { ... } @array;
91             #pod
92             #pod Return the index of the first item that matches the code block, or C<-1> if
93             #pod none match
94             #pod
95             #pod =cut
96              
97             # This duplicates List::Util firstidx, but this is not included in Perl 5.10
98             sub firstidx(&@) {
99 7     7 1 12 my $code = shift;
100 7         20 for my $i ( 0 .. @_ ) {
101 33         46 local $_ = $_[ $i ];
102 33 100       47 return $i if $code->();
103             }
104 0           return -1;
105             }
106              
107             1;
108              
109             __END__