File Coverage

blib/lib/again.pm
Criterion Covered Total %
statement 23 51 45.1
branch 1 16 6.2
condition 0 11 0.0
subroutine 7 12 58.3
pod 2 3 66.6
total 33 93 35.4


line stmt bran cond sub pod time code
1             package again;
2             $again::VERSION = '0.08';
3 1     1   15641 use strict;
  1         2  
  1         33  
4 1     1   4 use warnings;
  1         1  
  1         21  
5 1     1   14 use 5.006;
  1         5  
  1         30  
6 1     1   3 use Carp;
  1         2  
  1         210  
7              
8             my %mtimes;
9              
10             sub require_again {
11 0 0   0 1 0 @_ == 0 and croak 'Not enough arguments for require_again';
12 0 0       0 @_ > 1 and croak 'Too many arguments for require_again';
13 0         0 my $module = shift;
14 0         0 (my $file = "$module.pm") =~ s[::][/]g;
15 0 0 0     0 if (!exists($INC{$file}) || !exists($mtimes{$INC{$file}}) || -M $INC{$file} < $mtimes{$INC{$file}}) {
      0        
16 0         0 delete $INC{$file};
17 0         0 _unload_module($module);
18 0         0 require $file;
19 0         0 $mtimes{$INC{$file}} = -M $INC{$file};
20             }
21             }
22              
23             # Unload all entries in the symbol table, so we get a clean load
24             # and don't get any warnings about subs being redefined
25             # This function was borrowed from Class::Unload by Dagfinn Ilmari MannsÃ¥ker
26             sub _unload_module
27             {
28 0     0   0 my $package = shift;
29 0         0 my $symbol_table = $package.'::';
30              
31 1     1   4 no strict 'refs';
  1         1  
  1         223  
32 0         0 foreach my $symbol (keys %$symbol_table) {
33 0 0       0 next if $symbol =~ /\A[^:]+::\z/;
34 0         0 delete $symbol_table->{$symbol};
35             }
36             }
37              
38             sub use_again {
39 0     0 0 0 croak '"use_again" should be "use again"';
40             }
41              
42             sub use {
43 0     0 1 0 my $method = shift;
44 0 0       0 $_[0] or croak 'Not enough arguments for use again';
45 0         0 require_again($_[0]);
46 0 0 0     0 if (@_ == 2 and ref $_[1] eq 'ARRAY') {
47 0 0       0 return if @{ $_[1] } == 0;
  0         0  
48 0         0 splice @_, 1, 1, @{ $_[1] };
  0         0  
49             }
50 0   0     0 goto $_[0]->can($method) || return;
51             }
52              
53             sub import {
54 1 50   1   8 if (@_ > 1) {
55 0         0 splice @_, 0, 1, 'import';
56 0         0 goto &use;
57             }
58 1     1   4 no strict 'refs';
  1         1  
  1         133  
59 1         2 *{caller() . "::use_again"} = \&use_again;
  1         4  
60 1         1 *{caller() . "::require_again"} = \&require_again;
  1         9  
61             }
62              
63             sub unimport {
64 0     0     splice @_, 0, 1, 'unimport';
65 0           goto &use;
66             }
67              
68             1;
69              
70             =head1 NAME
71              
72             again - mechanism for manually reloading modules when they've changed
73              
74             =head1 SYNOPSIS
75              
76             use again 'LWP::Simple'; # default import
77             use again 'LWP::Simple', []; # no import
78             use again 'LWP::Simple', [qw(get)]; # import only get
79            
80             use again 'LWP::Simple', (); # default import (!!)
81             use again 'LWP::Simple', qw(get); # import only get
82            
83             use again;
84             require_again 'Foo::Bar';
85              
86             =head1 DESCRIPTION
87              
88             This module provides a mechanism for manually reloading a module
89             if its file has changed since it was first / previously loaded.
90             This can be useful for long-running applications, where new versions of
91             modules might be installed while the application is still running.
92              
93             =head2 Usage
94              
95             =over
96              
97             =item C
98              
99             A bare C, with no import list,
100             will export C into your package.
101             For historical reasons it will also export C,
102             which you shouldn't use (it will croak anyway).
103              
104             =item C
105              
106             If you do pass arguments, the first is used with C, and all
107             remaining arguments are used to import symbols into your namespace.
108              
109             When given arguments, C does not export its own functions.
110              
111             A single array reference is flattened. If that arrayref contains no elements,
112             the import does not take place.
113              
114             In mod_perl scripts, this of course only happens when your script is Ced.
115             This happens when your Apache::Registry or Apache::PerlRun script changes, or
116             when your PLP script is requested.
117              
118             =item C
119              
120             This is the driving force behind C. It Cs your module if it
121             has not been loaded with C before or it has changed since the
122             last time C loaded it.
123              
124             If you're imported a function from the module,
125             then you'll need to re-import it after calling C:
126              
127             use again 'Module::Path', qw(module_path);
128              
129             ... do some stuff ...
130              
131             require_again('Module::Path');
132             Module::Path->import('module_path');
133              
134             If you don't do this then you'll end up running the version of the
135             function that you first loaded.
136              
137             =back
138              
139             =head1 SEE ALSO
140              
141             L provides a class method which checks all
142             loaded modules to see if the file on disk has changed since the module
143             was loaded.
144              
145             L unloads a class, by clearing out its symbol table
146             and removing it from C<%INC>.
147              
148             L is part of the L IDE.
149             It's similar to L,
150             but says it has "a few more tricks up its sleeve".
151             It's not documented though,
152             so just intended for internal use in Padre.
153              
154             =head1 REPOSITORY
155              
156             L
157              
158             =head1 LICENSE
159              
160             There is no license. This software was released into the public domain.
161             Do with it what you want, but on your own risk. The author disclaims any
162             responsibility.
163              
164             If you want to (re)distribute this module and need a license,
165             you can redistribute it and/or modify it under the same terms as Perl itself.
166              
167             =head1 AUTHOR
168              
169             Juerd Waalboer Ejuerd@cpan.orgE Ehttp://juerd.nl/E
170              
171             Documentation updates from Neil Bowers.
172              
173             =cut