lib/Chj/ruse.pm

filter: Statementstmt 33.0% 40 / 121 Branchbran 1.9% 1 / 52 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 3 totaltotal 25.3% 50 / 197 CC 32 SCAR 35.6
CC
32
Cov
23%
check
90.0
import
42.7
reimport
42.0
CRAP 35.1
« lib/Chj/IO/Tempdir.pm ↑ Summary lib/Chj/xperlfunc.pm »
1 #
2 # Copyright (c) 2004-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 Chj::ruse - reload modules
13  
14 =head1 SYNOPSIS
15  
16 use FP::Repl;
17 use Foo;
18 use Chj::ruse;
19 use Bar qw(biz bim);
20 repl;
21 # edit the Foo.pm or Bar.pm files, then (possibly from the repl):
22 > ruse; # reloads all changed modules, and re-does all imports
23 # which have happened for those modules since Chj::ruse has
24 # been loaded.
25  
26 use Chj::ruse 'r'; # imports alias `r` for `ruse` (shorter to type)
27  
28 =head1 DESCRIPTION
29  
30 Extended copy of Module::Reload which modifies Exporter.pm so
31 that exports are tracked, so that these are redone as well.
32  
33 One function is exported: ruse. It does the equivalent
34 of Module::Reload->check, and re-exports stuff as far as possible.
35  
36 The function takes an optional argument: a temporary new debug level,
37 which shadows the default one stored in $Chj::ruse::DEBUG.
38 0 means no debugging info. -1 means be very silent (set $^W to false,
39 to prevent redefinitions of subroutines *which are NOT in the namespace
40 being reloaded* (subroutines in the namespace being reoaded are deleted
41 first, so there is never given a warning in this 'normal' case)).
42  
43 =head1 BUGS
44  
45 Each time import is called on a particular modules ("use Foo qw(biz baz)"),
46 the previous import arguments from the previous call is forgotten.
47 Thus if a module is use'd/import'ed multiple times in a row from the
48 same source file, only part of the import list is remembered. Thus
49 not everything is re-exported by ruse.
50 (Can this be solved?)
51  
52 Hm, if an error prevented a module from having been loaded, somehow
53 reload doesn't (always?) work ? why?
54  
55 This module might have problems with threads - I don't know if
56 other threads might try to run subroutines which have been deleted before
57 being defined again.
58  
59 =head1 NOTE
60  
61 This is alpha software! Read the status section in the package README
62 or on the L<website|http://functional-perl.org/>.
63  
64 =cut
65  
66 package Chj::ruse;
67 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 our $DEBUG = 0
74 ; # -1 = more than normal-silent. 0 = no debugging. 1,2,3= debugging levels.
75  
76 1 our $orig_import = \&Exporter::import;
77  
78 1 our %rdep; # moduleclassname => caller => [ import-arguments ]
79  
80 107 ▶sub new_import {
Subroutine new_import: called CC=2 SCAR=7.0
Pod undocumented
81 107 ▶ warn "new_import called" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::DEBUG > 2 0 107 107
82 107 my $caller = caller;
83 107 my ($class) = @_;
84 107 $rdep{$class}{$caller} = [@_];
85 107 goto &$orig_import;
86 }
87  
88 {
89 1 ▶ no warnings "redefine";
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 local $^W = ($DEBUG > 0);
91 1 *Exporter::import = \&new_import;
92 }
93  
94 {
95  
96 1 package Chj::ruse::Reload;
97  
98 # modified copy from Module::Reload
99  
100 1 our %Stat;
101 1 our $Debug;
102  
103 0 ▶ sub wipeout_namespace {
Subroutine wipeout_namespace: not called CC=4 SCAR=25.8
104 0 my ($key) = @_;
105 0 my $class = $key;
106 0 $class =~ s|/|::|sg; ##COPY!!below.
107 0 $class =~ s|\.pm$||s;
108 0 my $h = do {
109 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 0 \%{"${class}::"}
111 };
112 0 for (keys %$h) {
113 0 ▶ unless (/[^:]::\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/[^:]::\z/) 0 0 0
114 0 delete $$h{$_};
115 0 ▶ warn "deleted '$_'" if $Chj::ruse::DEBUG > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::DEBUG > 0 0 0 0
116 }
117 }
118 }
119  
120 0 ▶ sub check {
Subroutine check: not called CC=9 SCAR=45.0
121 0 $Debug
122 = $Chj::ruse::DEBUG; # so that it works when that one's local'ized
123 0 my $c = 0;
124 0 my @ignores;
125 push @ignores, $INC{"Module/Reload.pm"}
126 0 ▶ if exists $INC{"Module/Reload.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{"Module/Reload.pm"} 0 0 0
127 0 ▶ push @ignores, $INC{"Chj/ruse.pm"} if exists $INC{"Chj/ruse.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{"Chj/ruse.pm"} 0 0 0
128 0 local $^W = ($Debug >= 0);
129 0 ▶ my $memq_ignores = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
130 0 my ($f) = @_;
131 0 for (@ignores) {
132 0 ▶ return 1 if $_ eq $f
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $f 0 0 0
133 }
134 0
135 0 };
136 0 my %inc = %INC;
137 0 ▶ while (my ($key, $file) = each %inc) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %inc) 0 0 0
138 0 ▶ my $reload = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
139 0 delete $INC{$key};
140 0 wipeout_namespace($key);
141 0 eval {
142 0 local $SIG{__WARN__} = \&warn; # (cj: what does that do?)
143 0 require $key;
144 };
145 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
146 0 warn "Module::Reload: error during reload of '$key': $@\n"
147 } else {
148 0 ▶ if ($Debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Chj::ruse::Reload::Debug > 0) 0 0 0
149 0 ▶ warn "Module::Reload: process $$ reloaded '$key'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::Reload::Debug == 1 0 0 0
150 if $Debug == 1;
151 0 ▶ warn(
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::Reload::Debug >= 2 0 0 0
152 "Module::Reload: process $$ reloaded '$key' (\@INC = "
153 . join(', ', @INC) . ")\n")
154 if $Debug >= 2;
155 }
156 0 Chj::ruse::reimport($key);
157 }
158 0 ++$c;
159 0 };
160 0 ▶ if (!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file) { } 0 0 0
161 0 &$reload;
162 0 my $file2 = $INC{$key};
163 0 my $mtime = (stat $file2)[9];
164 0 $Stat{$file2} = $mtime;
165 } else {
166 0 ▶ next if $memq_ignores->($file); # too confusing
Branch: 1 branch
BranchTrueFalseTotal
if &$memq_ignores($file) 0 0 0
167 #local $^W = 0; XX nope, only shut down redefinition warnings please.
168 0 my $mtime = (stat $file)[9];
169 0 ▶ $Stat{$file} = $^T unless defined $Stat{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Chj::ruse::Reload::Stat{$file} 0 0 0
170 0 ▶ warn "Module::Reload: stat '$file' got $mtime >? $Stat{$file}\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::Reload::Debug >= 3 0 0 0
171 if $Debug >= 3;
172 0 ▶ &$reload if ($mtime > $Stat{$file});
Branch: 1 branch
BranchTrueFalseTotal
if $mtime > $Chj::ruse::Reload::Stat{$file} 0 0 0
173 0 $Stat{$file} = $mtime;
174  
175 # (XX shouldn't let it warn forever if it couldn't reload?)
176 }
177 }
178 0 $c;
179 }
180 }
181  
182 1 our $verbose = 0;
183  
184 0 ▶sub reimport {
Subroutine reimport: not called CC=6 SCAR=37.4
Pod undocumented
185 0 my ($key) = @_;
186 0 my $class = $key;
187 0 $class =~ s|/|::|sg; ##COPY above !!
188 0 $class =~ s|\.pm$||s;
189 0 ▶ if (my $importer = $class->can("import")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $importer = $class->can('import')) { } 0 0 0
190 0 my $imports = $rdep{$class};
191 0 for my $caller (keys %$imports) {
192 0 my $code = "package $caller; "
193 . '$Chj::ruse::orig_import->(@{$$imports{$caller}})';
194 0 eval $code; # XX is this safe? (security)
195 0 ▶ if (ref $@ or $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ or $@) 0 0 0
Condition0%0/3
ref $@ or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $@ or $@
ref $@ $@
Truth table0%0/3
A or B
A= ref $@B= $@
ABresult
1X1
011
000
196 0 warn "reimport WARNING: evaling '$code' gave: $@";
197 }
198 }
199 } else {
200 0 ▶ warn( "reimport WARNING: $class->can('import') didn't yield true, "
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::verbose 0 0 0
201 . "apparently the module doesn't inherit from Exporter")
202 if $verbose;
203 }
204 }
205  
206 0 ▶sub ruse {
Subroutine ruse: not called CC=3 SCAR=24.8
Pod undocumented
207 0 ▶ @_ > 1 and croak "ruse only takes 0 or 1 arguments";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and croak("ruse only takes 0 or 1 arguments") 0 0 0
208 0 ▶ local $DEBUG = (@_ ? $_[0] : $DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
209 0 Chj::ruse::Reload->check;
210 }
211  
212 0 ▶sub import {
Subroutine import: not called CC=7 SCAR=37.5
213 0 my $class = shift;
214 0 my $caller = caller;
215 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 0 ▶ warn "Copying ruse function to '${caller}::ruse'" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::ruse::DEBUG > 1 0 0 0
217 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
218 0 for my $name (@_) {
219 0 ▶ if ($name eq 'r') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'r') { } 0 0 0
elsif ($name eq 'ruse') { } 0 0 0
elsif ($name eq ':all') { } 0 0 0
220 0 *{"${caller}::r"} = \&ruse;
221 } elsif ($name eq 'ruse') {
222 0 *{"${caller}::ruse"} = \&ruse;
223 } elsif ($name eq ':all') {
224 0 *{"${caller}::r"} = \&ruse;
225 0 *{"${caller}::ruse"} = \&ruse;
226 } else {
227 0 die "no such export: $name";
228 }
229 }
230 } else {
231 0 *{"${caller}::ruse"} = \&ruse;
232 }
233 }
234  
235 1 1
« lib/Chj/IO/Tempdir.pm ↑ Summary lib/Chj/xperlfunc.pm »