| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package ETL::Yertl::Util; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.037'; | 
| 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 | 27 |  |  | 27 |  | 154 | use ETL::Yertl; | 
|  | 27 |  |  |  |  | 49 |  | 
|  | 27 |  |  |  |  | 113 |  | 
| 14 | 27 |  |  | 27 |  | 767 | use Exporter qw( import ); | 
|  | 27 |  |  |  |  | 47 |  | 
|  | 27 |  |  |  |  | 650 |  | 
| 15 | 27 |  |  | 27 |  | 119 | use Module::Runtime qw( use_module compose_module_name ); | 
|  | 27 |  |  |  |  | 43 |  | 
|  | 27 |  |  |  |  | 108 |  | 
| 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 | 281 |  |  | 281 | 1 | 650 | my ( $type, $name ) = @_; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 281 | 50 |  |  |  | 647 | die "$type is required\n" unless $name; | 
| 39 | 281 |  |  |  |  | 483 | my $class = eval { compose_module_name( 'ETL::Yertl::' . ucfirst $type, $name ) }; | 
|  | 281 |  |  |  |  | 1216 |  | 
| 40 | 281 | 100 |  |  |  | 12149 | if ( $@ ) { | 
| 41 | 1 |  |  |  |  | 5 | die "Unknown $type '$name'\n"; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 280 |  |  |  |  | 402 | eval { | 
| 45 | 280 |  |  |  |  | 1042 | use_module( $class ); | 
| 46 |  |  |  |  |  |  | }; | 
| 47 | 280 | 100 |  |  |  | 7346 | if ( $@ ) { | 
| 48 | 1 | 50 |  |  |  | 7 | 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 | 279 |  |  |  |  | 1358 | 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 | 410 |  |  | 410 | 1 | 961 | my ( @array ) = @_; | 
| 68 | 410 |  |  |  |  | 589 | my @pairs; | 
| 69 | 410 |  |  |  |  | 910 | while ( @array ) { | 
| 70 | 1204 |  |  |  |  | 2714 | push @pairs, [ shift( @array ), shift( @array ) ]; | 
| 71 |  |  |  |  |  |  | } | 
| 72 | 410 |  |  |  |  | 1200 | 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 | 382 | return map $_[$_], grep { $_ % 2 == 0 } 0..$#_; | 
|  | 14 |  |  |  |  | 36 |  | 
| 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 | 14 | my $code = shift; | 
| 100 | 7 |  |  |  |  | 19 | for my $i ( 0 .. @_ ) { | 
| 101 | 33 |  |  |  |  | 45 | local $_ = $_[ $i ]; | 
| 102 | 33 | 100 |  |  |  | 56 | return $i if $code->(); | 
| 103 |  |  |  |  |  |  | } | 
| 104 | 0 |  |  |  |  |  | return -1; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | 1; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | __END__ |