develooper Front page | perl.perl5.porters | Postings from April 2000

lib/ByteCache.pm

From:
simon.p.cozens
Date:
April 3, 2000 23:26
Subject:
lib/ByteCache.pm
Message ID:
CA2568B7.0022BDC6.00@au-aapmta001.aap.pwcinternal.com
This is lib/ByteCache.pm
It does just-in-time bytecode compilation of modules.
Enjoy.

Simon

package ByteCache;

BEGIN {
use Config ();
use File::Spec::Functions ();
unshift @INC,
File::Spec::Functions::catdir($Config::Config{sitearch},"byte"),
\&ByteCache::bytecacher;
}

use strict;
use warnings;
use File::Path ();
use File::Basename ();

my $caching=1; # The big off switch.

sub bytecacher {
     # Woah, don't fall into that trap again
     return undef if $_[1] eq "ByteLoader.pm" or $_[1] eq "XSLoader.pm";
     return undef unless $caching;

     my $current;
     for (@INC) {
          if (-e ($current = File::Spec::Functions::catfile($_,$_[1]))) {
               # Bytecompile, store and return filehandle.
               my $output=
                    File::Spec::Functions::catfile(
                         $Config::Config{sitearch}, "byte", $_[1]
                    );
               my $outputdir = File::Basename::dirname($output);
               unless (-d $outputdir or File::Path::mkpath($outputdir)) {
                    warn "Can't create $outputdir, not byte caching.\n";
                    return undef;
               }
               warn "Compiling $_[1]\n";
               if (system("perlcc -B -o $output $current") < 0) {
                    warn "Couldn't call the compiler.\n";
                    $caching=0;
               }
               if (-e $output) {
                    open(FH, $output);
                    binmode FH;
                    return *FH;
               }
          }
     }
     return undef;
}

1;

=head1 NAME

ByteCache - byte-compile modules when needed

=head1 SYNOPSIS

     use ByteCache;
     use Other::Module;

=head1 DESCRIPTION

This module causes any modules loaded after it to be loaded in bytecode
compiled format. If a bytecode compiled version of the module does not
currently exist, ByteCache will call the compiler to create one and
then save it away.

=head1 WARNING

This module is dependent on the compiler suite, and is therefore B<very>
experimental. Your results may very. Do not use in production systems.

=head1 AUTHOR

Simon Cozens, <simon@brecon.co.uk>

=head1 SEE ALSO

L<perl>, L<perlcc>, L<ByteLoader>
----------------------------------------------------------------
The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged
material.  Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by persons or
entities other than the intended recipient is prohibited.   If you received
 this in error, please contact the sender and delete the material from any
computer.





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About