File Coverage

blib/lib/ByteCache.pm
Criterion Covered Total %
statement 34 39 87.1
branch 8 12 66.6
condition 2 6 33.3
subroutine 8 8 100.0
pod 0 1 0.0
total 52 66 78.7


line stmt bran cond sub pod time code
1             package ByteCache;
2              
3             require 5.6.0;
4 1     1   581 use strict;
  1         2  
  1         35  
5 1     1   5 use warnings;
  1         1  
  1         46  
6             our $VERSION = '0.01';
7              
8             BEGIN {
9 1     1   5 use Config ();
  1         4  
  1         23  
10 1     1   909 use File::Spec::Functions ();
  1         960  
  1         53  
11 1     1   1012 unshift @INC,
12             File::Spec::Functions::catdir($Config::Config{sitearch},"byte"),
13             \&ByteCache::bytecacher;
14              
15 1     1   5 use File::Path ();
  1         2  
  1         17  
16 1     1   4 use File::Basename ();
  1         2  
  1         303  
17              
18 1         3947 my $caching=1; # The big off switch.
19              
20             sub bytecacher {
21             # Woah, don't fall into that trap again
22 6 50 33 6 0 113540 return undef if $_[1] eq "ByteLoader.pm" or $_[1] eq "XSLoader.pm";
23 6 100       5016 return undef unless $caching;
24              
25 1         2 my $current;
26 1         3 for (@INC) {
27 13 100       404 if (-e ($current = File::Spec::Functions::catfile($_,$_[1]))) {
28             # Bytecompile, store and return filehandle.
29 1         15 my $output=
30             File::Spec::Functions::catfile(
31             $Config::Config{sitearch}, "byte", $_[1]
32             );
33 1         86 my $outputdir = File::Basename::dirname($output);
34 1 50 33     341 unless (-d $outputdir or File::Path::mkpath($outputdir)) {
35 0         0 warn "Can't create $outputdir, not byte caching.\n";
36 0         0 return undef;
37             }
38 1         42 warn "Compiling $_[1]\n";
39 1 50       3310 if (system("perlcc -B -o $output $current") < 0) {
40 1         51 warn "Couldn't call the compiler.\n";
41 1         8 $caching=0;
42             }
43 1 50       98 if (-e $output) {
44 0         0 open(FH, $output);
45 0         0 binmode FH;
46 0         0 return *FH;
47             }
48             }
49             }
50 1         1276 return undef;
51             }
52             }
53              
54             1;
55              
56             =head1 NAME
57              
58             ByteCache - byte-compile modules when needed
59              
60             =head1 SYNOPSIS
61              
62             use ByteCache;
63             use Other::Module;
64              
65             =head1 DESCRIPTION
66              
67             This module causes any modules loaded after it to be loaded in bytecode
68             compiled format. If a bytecode compiled version of the module does not
69             currently exist, ByteCache will call the compiler to create one and
70             then save it away.
71              
72             =head1 WARNING
73              
74             This module is dependent on the compiler suite, and is therefore B<very>
75             experimental. Your results may very. Do not use in production systems.
76              
77             =head1 AUTHOR
78              
79             Simon Cozens, C
80              
81             =head1 SEE ALSO
82              
83             L, L, L