-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastLoad.pm
50 lines (37 loc) · 1019 Bytes
/
FastLoad.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package FastLoad;
my $cachefile;
my $cache;
BEGIN {
# load the cache
$cachefile = '.fastload';
my $code = do { open my $fh, $cachefile; local $/; <$fh> };
eval $code;
# load the hook
unshift @INC, \&fastload unless grep { "$_" eq \&fastlog . '' } @INC;
}
sub fastload {
my ( $code, $module ) = @_;
# ensure our hook remains first in @INC
@INC = ( $code, grep { $_ ne $code } @INC )
if $INC[0] ne $code;
# if we know where the source is, just load it
if ( exists $cache->{$module} ) {
open my $fh, $cache->{$module} or return;
return $fh;
}
# let Perl ultimately find the required file
return;
}
END {
# update cache with content of %INC
while ( my ( $module, $file ) = each %INC ) {
next if ref $file;
next if !-e $file;
$cache->{$module} = $file;
}
# save cache
open my $fh, '>', $cachefile;
require Data::Dumper;
print $fh Data::Dumper->Dump( [$cache], ['$cache'] );
}
1;