File Coverage

blib/lib/again.pm
Criterion Covered Total %
statement 20 42 47.6
branch 1 14 7.1
condition 0 8 0.0
subroutine 6 10 60.0
pod 2 3 66.6
total 29 77 37.6


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