| 1 | package again; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $again::VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | my %mtimes; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub require_again { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
require_again: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | @_ == 0 and croak 'Not enough arguments for require_again'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | @_ > 1 and croak 'Too many arguments for require_again'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | my $module = shift; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | (my $file = "$module.pm") =~ s[::][/]g; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | if (!exists($INC{$file}) || !exists($mtimes{$INC{$file}}) || -M $INC{$file} < $mtimes{$INC{$file}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not exists $INC{$file} && exists $mtimes{$INC{$file}} or -M $INC{$file} < $mtimes{$INC{$file}}
exists $INC{$file} && exists $mtimes{$INC{$file}}
MC/DC0%0/3
not exists $INC{$file} && exists $mtimes{$INC{$file}} or -M $INC{$file} < $mtimes{$INC{$file}}
exists $INC{$file}
exists $mtimes{$INC{$file}}
-M $INC{$file} < $mtimes{$INC{$file}}
Truth table0%0/4
not(A && B) or C
A= exists $INC{$file}B= exists $mtimes{$INC{$file}}C= -M $INC{$file} < $mtimes{$INC{$file}}
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | delete $INC{$file}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | _unload_module($module); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | require $file; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 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 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unload_module: not called CC=3 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $package = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $symbol_table = $package.'::'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | foreach my $symbol (keys %$symbol_table) { | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | next if $symbol =~ /\A[^:]+::\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | delete $symbol_table->{$symbol}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub use_again { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
use_again: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | croak '"use_again" should be "use again"'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub use { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
use: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $_[0] or croak 'Not enough arguments for use again'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | require_again($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (@_ == 2 and ref $_[1] eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 and ref $_[1] eq "ARRAY"
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "ARRAY"
@_ == 2
ref $_[1] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[1] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return if @{ $_[1] } == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | splice @_, 1, 1, @{ $_[1] }; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | goto $_[0]->can($method) || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]->can($method) || (return)
MC/DC0%0/1
$_[0]->can($method) || (return)
$_[0]->can($method)
Truth table0%0/2
A
A= $_[0]->can($method)
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | splice @_, 0, 1, 'import'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | goto &use; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | *{caller() . "::use_again"} = \&use_again; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | *{caller() . "::require_again"} = \&require_again; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub unimport { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | splice @_, 0, 1, 'unimport'; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | goto &use; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | 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<use again;> | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | A bare C<use again;>, with no import list, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | will export C<require_again> into your package. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | For historical reasons it will also export C<use_again>, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | which you shouldn't use (it will croak anyway). | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =item C<use again MODULE, [ IMPORTS ];> | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | If you do pass arguments, the first is used with C<require_again>, and all | ||||||||||||||||||||||||||||||||||||||||||
| 107 | remaining arguments are used to import symbols into your namespace. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | When given arguments, C<use again> 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 C<eval>ed. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | This happens when your Apache::Registry or Apache::PerlRun script changes, or | ||||||||||||||||||||||||||||||||||||||||||
| 116 | when your PLP script is requested. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =item C<require_again MODULE;> | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | This is the driving force behind C<again.pm>. It C<require>s your module if it | ||||||||||||||||||||||||||||||||||||||||||
| 121 | has not been loaded with C<require_again> before or it has changed since the | ||||||||||||||||||||||||||||||||||||||||||
| 122 | last time C<require_again> 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<require_again>: | ||||||||||||||||||||||||||||||||||||||||||
| 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<Module::Reload> 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<Class::Unload> unloads a class, by clearing out its symbol table | ||||||||||||||||||||||||||||||||||||||||||
| 146 | and removing it from C<%INC>. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | L<Padre::Unload> is part of the L<Padre> IDE. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | It's similar to L<Class::Unload>, | ||||||||||||||||||||||||||||||||||||||||||
| 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<https://github.com/neilbowers/again> | ||||||||||||||||||||||||||||||||||||||||||
| 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 E<lt>juerd@cpan.orgE<gt> E<lt>http://juerd.nl/E<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | Documentation updates from Neil Bowers. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||||||||||||||||