File Coverage

blib/lib/Date/Transform/Closures.pm
Criterion Covered Total %
statement 42 42 100.0
branch 1 2 50.0
condition n/a
subroutine 13 13 100.0
pod 0 3 0.0
total 56 60 93.3


line stmt bran cond sub pod time code
1             package Date::Transform::Closures;
2            
3 1     1   31 use 5.006;
  1         3  
  1         38  
4 1     1   5 use strict;
  1         2  
  1         32  
5 1     1   5 use warnings;
  1         1  
  1         34  
6 1     1   5 use Carp;
  1         1  
  1         83  
7 1     1   5 use Switch 'Perl6';
  1         2  
  1         9  
8 1     1   20926 use Tie::IxHash;
  1         3  
  1         56  
9            
10             require Exporter;
11 1     1   974 use AutoLoader qw(AUTOLOAD);
  1         1729  
  1         6  
12             our @ISA = qw( Exporter );
13            
14             # Items to export into callers namespace by default. Note: do not export
15             # names by default without a very good reason. Use EXPORT_OK instead.
16             # Do not simply export all your public functions/methods/constants.
17            
18             # This allows declaration use Date::Transform ':all';
19             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20             # will save memory.
21             our %EXPORT_TAGS =
22             ( 'all' => [qw( mk_set_filter_input mk_passthru mk_function)] );
23             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24             our @EXPORT = qw( mk_set_filter_input mk_passthru mk_function );
25            
26             our $VERSION = '0.11';
27            
28             ## SUBROUTINE: mk_set_filter_input
29             ## Generates a function that sets the key of Tie::IxHash
30             ## The key of the object is set to the value of the evaluated function.
31             ##
32             ## Usage: $fn = mk_set_filter_input( $key, $f);
33             ## Tie::IxHash_obj->$fn
34             ##
35             sub mk_set_filter_input {
36            
37 6     6 0 11 my $key = shift;
38 6         8 my $function = shift;
39            
40             my $new_function =
41            
42             sub {
43            
44 6     6   9 my $self = shift;
45 6         21 $self->{filter}->{input}
46             ->STORE( $key, $self->{filter}->{matches}->$function );
47            
48 6         29 };
49            
50 6         15 return $new_function;
51             }
52            
53             ## SUBROUTINE: mk_passthrough
54             ## Generates a function that returns the value of the Tie::IxHash
55             ## The value returned is specified by the passed argument key.
56             ## * In the anonymous function, $self will be a Tie::IxHash object,
57             ## Input Object.
58             ##
59             ## Usage: $fn = mk_passthru( $key );
60             ## Tie::IxHash_obj->$fn
61             sub mk_passthru {
62            
63 4     4 0 8 my $key = shift;
64 4 50       11 carp("No key provided for passthru\n") if ( !defined $key );
65            
66             my $function = sub {
67            
68 4     4   6 my $self = shift;
69 4         15 my $value = $self->FETCH($key);
70            
71 4         37 return $value;
72            
73 4         21 };
74            
75 4         12 return $function;
76             }
77            
78             ## SUBROUTINE: mk_function
79             ## returns a closure that applies $function to the value(s)
80             ## of the key(s), @keys
81             ##
82             ## usage: mk_function( ref_to_function, Tie::IxHash_keys object );
83             ##
84             sub mk_function {
85            
86 2     2 0 13 my $function = shift;
87 2         6 my @keys = @_;
88            
89             ## print "@keys\n";
90            
91             my $new_function = sub {
92            
93 2     2   4 my $matches = shift;
94 2         5 my @inputs = map { $matches->FETCH($_) } @keys;
  3         16  
95            
96 2         23 return &$function(@inputs);
97            
98 2         8 };
99            
100 2         6 return $new_function;
101            
102             } # END SUBROUTINE: mk_function
103            
104             1;
105            
106             __END__;