File: Bytecode.pm

package info (click to toggle)
libpar-perl 0.952-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,068 kB
  • ctags: 512
  • sloc: perl: 14,520; ansic: 870; makefile: 57
file content (85 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package PAR::Filter::Bytecode;

use strict;
use base 'PAR::Filter';
use File::Temp ();

=head1 NAME

PAR::Filter::Bytecode - Bytecode filter

=head1 SYNOPSIS

    PAR::Filter::Bytecode->apply(\$code); # transforms $code

=head1 DESCRIPTION

This filter uses L<B::Bytecode> to turn the script into comment-free,
architecture-specific Perl bytecode, and uses L<ByteLoader> to load
back on execution.

For L<pp> users, please add an extra B<-M> option, like this:

    pp -f Bytecode -M ByteLoader

Otherwise, the implicit dependency on ByteLoader will not be detected.

=head1 CAVEATS

This backend exhibits all bugs listed in L<B::Bytecode>, and then some.

Bytecode support is considered to be extremely fragile on Perl versions
earlier than 5.8.1, and is still far from robust (as of this writing).

=cut

sub apply {
    my $ref = $_[1];

    my ($fh, $in_file) = File::Temp::tempfile();
    print $fh $$ref;
    close $fh;

    my $out_file = File::Temp::tmpnam();
    system($^X, "-MO=Bytecode,-H,-k,-o$out_file", $in_file);
    unless (-e $out_file) {
	warn "Cannot transform $in_file to $out_file: $! ($?)\n";
	return;
    }

    unlink($in_file);

    open my $fh, '<', $out_file or die $!;
    local $/;
    $$ref = <$fh>;
    close $fh;

    unlink($out_file);
}

1;

=head1 SEE ALSO

L<PAR::Filter>, L<B::Bytecode>, L<ByteLoader>

=head1 AUTHORS

Audrey Tang E<lt>cpan@audreyt.orgE<gt>

L<http://par.perl.org/> is the official PAR website.  You can write
to the mailing list at E<lt>par@perl.orgE<gt>, or send an empty mail to
E<lt>par-subscribe@perl.orgE<gt> to participate in the discussion.

Please submit bug reports to E<lt>bug-par@rt.cpan.orgE<gt>.

=head1 COPYRIGHT

Copyright 2003, 2004, 2005, 2006 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut