File Coverage

blib/lib/lib/overlay.pm
Criterion Covered Total %
statement 26 30 86.6
branch 4 10 40.0
condition 1 3 33.3
subroutine 8 9 88.8
pod 0 2 0.0
total 39 54 72.2


line stmt bran cond sub pod time code
1             package lib::overlay;
2             $lib::overlay::VERSION = '0.00_01';
3              
4 1     1   35784 use strict;
  1         2  
  1         136  
5              
6             =head1 NAME
7              
8             lib::overlay - Overlay additional code on module loading
9              
10             =head1 SYNOPSIS
11              
12             use lib::overlay '_deprecated' => -warn;
13             use lib::overlay '_Overlay', '_Vendor', '_Local';
14              
15             =head1 DESCRIPTION
16              
17             Say you have C. Now C will, after loading
18             F, also try to look F<_deprecated/CGI.pm> in C<@INC> and load them too.
19              
20             Expect more documentation later. :-)
21              
22             =cut
23              
24             # each hook has a transformer, followed by any number of actions
25             my @pre_hooks;
26             my @post_hooks;
27              
28             BEGIN {
29 97     97 0 24474 sub do_require { require $_[0] }
30             *CORE::GLOBAL::require = sub (*) {
31 49     49   18454 run_hooks(\@pre_hooks, @_);
32 49         105 do_require(@_);
33 48         2364 run_hooks(\@post_hooks, @_);
34 1     1   352 };
35             }
36              
37             sub run_hooks {
38 97     97 0 111 my $hooks = shift;
39 97         179 foreach my $hook (@$hooks) {
40 48         53 my ($t, $a) = @{$hook}{'transform', 'action'};
  48         113  
41 48 50       101 my @args = $t->(@_) or next;
42              
43 48         80 foreach my $action (@$a) {
44 48         229 __PACKAGE__->can($action)->(@args);
45             }
46             }
47             }
48              
49             sub import {
50 1     1   11 my $class = shift;
51 1         5 while (@_) {
52 1         2 my $arg = shift;
53 1         1 my @actions;
54 1 50 33     5 if (@_ and $_[0] =~ /^-(\w+)/) {
55 0         0 push @actions, "_$1"; shift;
  0         0  
56 0 0       0 push @actions, shift if @_;
57             }
58 1 50       3 @actions = '_require' unless @actions;
59             push @post_hooks, {
60 48     48   190 transform => UNIVERSAL::isa($arg => 'CODE') ? $arg : sub { "$arg/$_[0]" },
61 1 50       35 action => \@actions,
62             }
63             }
64             }
65              
66             sub _require {
67 48     48   47 local $@; eval { do_require $_[0] };
  48         80  
  48         86  
68             }
69              
70 0     0     sub unimport {
71             }
72              
73             1;
74              
75             =head1 AUTHORS
76              
77             Autrijus Tang Eautrijus@autrijus.orgE
78              
79             =head1 COPYRIGHT
80              
81             Copyright 2004 by Autrijus Tang Eautrijus@autrijus.orgE
82              
83             This program is free software; you can redistribute it and/or
84             modify it under the same terms as Perl itself.
85              
86             See L
87              
88             =cut