File Coverage

blib/lib/Time/Duration/Filter.pm
Criterion Covered Total %
statement 62 62 100.0
branch 2 2 100.0
condition 6 7 85.7
subroutine 13 13 100.0
pod 0 1 0.0
total 83 85 97.6


line stmt bran cond sub pod time code
1             # Copyright 2009, 2010, 2011, 2013 Kevin Ryde
2              
3             # This file is part of Time-Duration-Locale.
4             #
5             # Time-Duration-Locale is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU General Public License as published
7             # by the Free Software Foundation; either version 3, or (at your option) any
8             # later version.
9             #
10             # Time-Duration-Locale is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13             # Public License for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Time-Duration-Locale. If not, see .
17              
18             package Time::Duration::Filter;
19 2     2   34 use 5.004;
  2         5  
  2         76  
20 2     2   9 use strict;
  2         3  
  2         60  
21 2     2   9 no strict 'refs';
  2         11  
  2         52  
22 2     2   84 use Carp;
  2         4  
  2         505  
23 2     2   11978 use Module::Load;
  2         5635  
  2         16  
24 2     2   123 use vars qw($VERSION $AUTOLOAD);
  2         3  
  2         315  
25              
26             # uncomment this to run the ### lines
27             #use Smart::Comments;
28              
29             $VERSION = 10;
30              
31             my @_target_ISA = ('Exporter');
32              
33             sub import {
34 2     2   7 shift; # $class own __PACKAGE__
35 2         14 setup (scalar caller(), @_);
36             }
37             sub setup {
38 2     2 0 10 my ($package, %options) = @_;
39 2   50     20 my $from = $options{'from'} || 'Time::Duration';
40 2         15 Module::Load::load ($from);
41              
42             ### setup: $package
43             ### $from
44 2     2   13 no strict;
  2         4  
  2         14961  
45 2         8722 *{"${package}::AUTOLOAD"} = \&_target_AUTOLOAD;
  2         29  
46 2         7 *{"${package}::can"} = \&_target_can;
  2         14  
47 2         3 ${"${package}::_from"} = $from;
  2         14  
48              
49 2         20 require Exporter;
50 2         5 *{"${package}::ISA"} = \@_target_ISA;
  2         21  
51             # same as the $from package
52 2         5 *{"${package}::EXPORT"} = \@{"${from}::EXPORT"};
  2         11  
  2         8  
53 2         5 *{"${package}::EXPORT_OK"} = \@{"${from}::EXPORT_OK"};
  2         10  
  2         7  
54 2         4 *{"${package}::EXPORT_TAGS"} = \@{"${from}::EXPORT_TAGS"};
  2         70  
  2         11  
55             }
56              
57             sub _target_can {
58 3     3   1842 my ($class, $name) = @_;
59 3   100     52 return ($class->SUPER::can($name) || _make_func ($class, $name));
60             }
61             sub _target_AUTOLOAD {
62             ### TDF AUTOLOAD of: $AUTOLOAD
63 13     13   1776 my ($package, $name) = ($AUTOLOAD =~ /(.*)::(.*)/);
64             my $subr = _make_func ($package, $name)
65 13   100     40 || die "No function $name exported by " . ${"${package}::_from"};
66 12         36 goto $subr;
67             }
68              
69             sub _make_func {
70 15     15   24 my ($package, $name) = @_;
71              
72 15 100       19 scalar(grep {$_ eq $name} @{"${package}::EXPORT_OK"})
  180         330  
  15         61  
73             or return; # no such function
74              
75 13         17 my $from = ${"${package}::_from"};
  13         41  
76 13         30 my $from_func = "${from}::$name";
77 13         51 my $filter_func = "${package}::_filter";
78             ### from: $from_func
79             ### filter: $filter_func
80              
81 13     20   57 my $subr = sub { &$filter_func (&$from_func (@_)) };
  20         691  
82 13         18 *{"${package}::$name"} = $subr;
  13         78  
83 13         117 return $subr;
84             }
85              
86              
87             # if ($name eq '_filter') {
88             # die "$package didn't define a _filter() function";
89             # }
90             # if ($name =~ /^_f/) {
91             # die "Oops, bad autoload $AUTOLOAD";
92             # }
93              
94             1;
95             __END__