/usr/share/perl5/Module/Compile.pm is in libmodule-compile-perl 0.35-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | # To Do:
#
# - Make preface part of parsed code, since it might contain `package`
# statements or other scoping stuff.
# - Build code into an AST.
use strict; use warnings;
package Module::Compile;
our $VERSION = '0.35';
use Digest::SHA();
# A lexical hash to keep track of which files have already been filtered
my $filtered = {};
# A map of digests to code blocks
my $digest_map = {};
# All subroutines are prefixed with pmc_ so subclasses don't
# accidentally override things they didn't intend to.
# Determine which stack frame points to the code we are filtering.
# This is a method in case it needs to be overridden.
sub pmc_caller_stack_frame { 0 };
# This is called while parsing source code to determine if the
# module/class in a use/no line is part of the Module::Compile game.
#
# Return true if this class supports PMC compilation.
#
# The hope is that this will allow interoperability with modules that
# do not inherit from Module::Compile but still want to do this sort
# of thing.
sub pmc_is_compiler_module { 1 };
sub new {
return bless {}, shift;
}
# This is called to determine whether the meaning of use/no is reversed.
sub pmc_use_means_no { 0 }
# This is called to determine whether the use line means a one line section.
sub pmc_use_means_now { 0 }
# All Module::Compile based modules inherit this import routine.
sub import {
my ($class) = @_;
return if $class->pmc_use_means_no;
goto &{$class->can('pmc_import')};
}
# Treat unimport like import if use means no
sub unimport {
my ($class) = @_;
return unless $class->pmc_use_means_no;
goto &{$class->can('pmc_import')};
}
sub pmc_import {
my ($class, @args) = @_;
# Handler modules can do `use Module::Compile -base;`. Make them ISA
# Module::Compile and get the hell out of Dodge.
$class->pmc_set_base(@args) and return;
my ($module, $line) = (caller($class->pmc_caller_stack_frame))[1, 2];
return if $filtered->{$module}++;
my $callback = sub {
my ($class, $content, $data) = @_;
my $output = $class->pmc_template($module, $content, $data);
$class->pmc_output($module, $output);
};
$class->pmc_check_compiled_file($module);
$class->pmc_filter($module, $line, $callback);
# Is there a meaningful return value here?
return;
}
# File might not be a module (.pm) and might be compiled already.
# If so, run the compiled file.
sub pmc_check_compiled_file {
my ($class, $file) = @_;
if (defined $file and $file !~ /\.pm$/i) {
# Do the freshness check ourselves
my $pmc = $file.'c';
$class->pmc_run_compiled_file($pmc), die
if -s $pmc and (-M $pmc <= -M $file);
}
}
sub pmc_run_compiled_file {
my ($class, $pmc) = @_;
my ($package) = caller($class->pmc_file_caller_frame());
eval "package $package; do \$pmc";
die $@ if $@;
exit 0;
}
sub pmc_file_caller_frame { 2 }
# Set up inheritance
sub pmc_set_base {
my ($class, $flag) = @_;
# Handle the `use Module::Compile -base;` command.
if ($class->isa(__PACKAGE__) and defined $flag and $flag eq '-base') {
my $descendant = (caller 1)[0];;
no strict 'refs';
push @{$descendant . '::ISA'}, $class;
return 1;
}
return 0;
}
# Generate the actual code that will go into the .pmc file.
sub pmc_template {
my ($class, $module, $content, $data) = @_;
my $base = __PACKAGE__;
my $check = $class->freshness_check($module);
my $version = $class->VERSION || '0';
return join "\n",
"# Generated by $class $version ($base $VERSION) - do not edit!",
"$check$content$data";
}
# This returns a piece of Perl code to do a runtime check to see if the
# .pmc file is fresh. By default we use a 32-bit running checksum.
sub freshness_check {
my ($class, $module) = @_;
my $sum = sprintf('%08X', do {
local $/;
open my $fh, "<", $module
or die "Cannot open $module: $!";
binmode($fh, ':crlf'); # normalize CRLF for consistent checksum
unpack('%32N*', <$fh>);
});
return << "...";
################((( 32-bit Checksum Validator III )))################
#line 1
BEGIN { use 5.006; local (*F, \$/); (\$F = __FILE__) =~ s!c\$!!; open(F)
or die "Cannot open \$F: \$!"; binmode(F, ':crlf'); if (unpack('%32N*',
\$F=readline(*F)) != 0x$sum) { use Filter::Util::Call; my \$f = \$F;
filter_add(sub { filter_del(); 1 while &filter_read; \$_ = \$f; 1; })}}
#line 1
...
}
# Write the output to the .pmc file
sub pmc_output {
my ($class, $module, $output) = @_;
$class->pmc_can_output($module)
or return 0;
my $pmc = $module . 'c';
# If we can't open the file, just return. The filtering will not be cached,
# but that might be ok.
open my $fh, ">", $pmc
or return 0;
# Protect against disk full or whatever else.
local $@;
eval {
print $fh $output
or die;
close $fh
or die;
};
if ( my $e = $@ ) {
# close $fh? die if unlink?
if ( -e $pmc ) {
unlink $pmc
or die "Can't delete errant $pmc: $!";
}
return 0;
}
return 1;
}
# Check whether output can be written.
sub pmc_can_output {
my ($class, $file_path) = @_;
return 1;
# return $file_path =~ /\.pm$/;
}
# We use a source filter to get all the code for compiling.
sub pmc_filter {
my ($class, $module, $line_number, $post_process) = @_;
# Read original module source code instead of taking from filter,
# because we need all the lines including the ones before the `use`
# statement, so we can parse Perl into packages and such.
open my $fh, $module
or die "Can't open $module for input:\n$!";
my $module_content = do { local $/; <$fh> };
close $fh;
# Find the real __DATA__ or __END__ line. (Not one hidden in a Pod
# section or heredoc).
my $folded_content = $class->pmc_fold_blocks($module_content);
my $folded_data = '';
if ($folded_content =~ s/^((?:__(?:DATA|END)__$).*)//ms) {
$folded_data = $1;
}
my $real_content = $class->pmc_unfold_blocks($folded_content);
my $real_data = $class->pmc_unfold_blocks($folded_data);
# Calculate the number of lines to skip in the source filter, since
# we already have them in $real_content.
my @lines = ($real_content =~ /(.*\n)/g);
my $lines_to_skip = @lines;
$lines_to_skip -= $line_number;
# Use filter to skip past that many lines
# Leave __DATA__ section intact
my $done = 0;
require Filter::Util::Call;
Filter::Util::Call::filter_add(sub {
return 0 if $done;
my $data_line = '';
while (1) {
my $status = Filter::Util::Call::filter_read();
last unless $status;
return $status if $status < 0;
# Skip lines up to the DATA section.
next if $lines_to_skip-- > 0;
if (/^__(?:END|DATA)__$/) {
# Don't filter the DATA section, or else the DATA file
# handle becomes invalid.
# XXX - Maybe there is a way to simply recreate the DATA
# file handle, or at least seek back to the start of it.
# Needs investigation.
# For now this means that we only allow compilation on
# the module content; not the DATA section. Because we
# want to make sure that the program runs the same way
# as both a .pm and a .pmc.
$data_line = $_;
last;
}
}
continue {
$_ = '';
}
$real_content =~ s/\r//g;
my $filtered_content = $class->pmc_process($real_content);
$class->$post_process($filtered_content, $real_data);
$filtered_content =~ s/(.*\n){$line_number}//;
$_ = $filtered_content . $data_line;
$done = 1;
});
}
use constant TEXT => 0;
use constant CONTEXT => 1;
use constant CLASSES => 2;
# Break the code into blocks. Compile the blocks.
# Fold out heredocs etc
# Parse the code into packages, blocks and subs
# Parse the code by `use/no *::Compiler`
# Build an AST
# Reduce the AST until fully reduced
# Return the result
sub pmc_process {
my $class = shift;
my $data = shift;
my @blocks = $class->pmc_parse_blocks($data);
while (@blocks = $class->pmc_reduce(@blocks)) {
if (@blocks == 1 and @{$blocks[0][CLASSES]} == 0) {
my $content = $blocks[0][TEXT];
$content .= "\n" unless $content =~ /\n\z/;
return $content;
}
}
die "How did I get here?!?";
}
# Analyze the remaining blocks and determine which compilers to call to reduce
# the problem.
#
# XXX This routine must do some kind of reduction each pass, or infinite loop
# will ensue. It is not yet certain if this is the case.
sub pmc_reduce {
my $class = shift;
my @blocks;
my $prev;
while (@_) {
my $block = shift;
my $next = $_[TEXT];
if ($next and "@{$block->[CLASSES]}" eq "@{$next->[CLASSES]}") {
shift;
$block->[TEXT] .= $next->[TEXT];
}
elsif (
(not $prev or @{$prev->[CLASSES]} < @{$block->[CLASSES]}) and
(not $next or @{$next->[CLASSES]} < @{$block->[CLASSES]})
) {
my $prev_len = $prev ? @{$prev->[CLASSES]} : 0;
my $next_len = $next ? @{$next->[CLASSES]} : 0;
my $offset = ($prev_len > $next_len) ? $prev_len : $next_len;
my $length = @{$block->[CLASSES]} - $offset;
$class->pmc_call($block, $offset, $length);
}
push @blocks, $block;
$prev = $block;
}
return @blocks;
}
# Call a set of compilers on a piece of source code.
sub pmc_call {
my $class = shift;
my $block = shift;
my $offset = shift;
my $length = shift;
my $text = $block->[TEXT];
my $context = $block->[CONTEXT];
my @classes = splice(@{$block->[CLASSES]}, $offset, $length);
for my $klass (@classes) {
local $_ = $text;
my $return = $klass->pmc_compile($text, ($context->{$klass} || {}));
$text = (defined $return and $return !~ /^\d+\z/)
? $return
: $_;
}
$block->[TEXT] = $text;
}
# Divide a Perl module into blocks. This code divides a module based on
# lines that use/no a Module::Compile subclass.
sub pmc_parse_blocks {
my $class = shift;
my $data = shift;
my @blocks = ();
my @classes = ();
my $context = {};
my $text = '';
my @parts = split /^([^\S\n]*(?:use|no)[^\S\n]+[\w\:\']+[^\n]*\n)/m, $data;
for my $part (@parts) {
if ($part =~ /^[^\S\n]*(use|no)[^\S\n]+([\w\:\']+)[^\n]*\n/) {
my ($use, $klass, $file) = ($1, $2, $2);
$file =~ s{(?:::|')}{/}g;
if ($klass =~ /^\d+$/) {
$text .= $part;
next;
}
{
local $@;
eval { require "$file.pm" };
die $@ if $@ and "$@" !~ /^Can't locate /;
}
if ($klass->can('pmc_is_compiler_module') and
$klass->pmc_is_compiler_module) {
push @blocks, [$text, {%$context}, [@classes]];
$text = '';
@classes = grep {$_ ne $klass} @classes;
if (($use eq 'use') xor $klass->pmc_use_means_no) {
push @classes, $klass;
$context->{$klass} = {%{$context->{$klass} || {}}};
$context->{$klass}{use} = $part;
if ($klass->pmc_use_means_now) {
push @blocks, ['', {%$context}, [@classes]];
@classes = grep {$_ ne $klass} @classes;
delete $context->{$klass};
}
}
else {
delete $context->{$klass};
}
}
else {
$text .= $part;
}
}
else {
$text .= $part;
}
}
push @blocks, [$text, {%$context}, [@classes]]
if length $text;
return @blocks;
}
# Compile/Filter some source code into something else. This is almost
# always overridden in a subclass.
sub pmc_compile {
my ($class, $source_code_string, $context_hashref) = @_;
return $source_code_string;
}
# Regexp fragments for matching heredoc, pod section, comment block and
# data section.
my $re_here = qr/
(?: # Heredoc starting line
^ # Start of some line
((?-s:.*?)) # $2 - text before heredoc marker
<<(?!=) # heredoc marker
[\t\x20]* # whitespace between marker and quote
((?>['"]?)) # $3 - possible left quote
([\w\-\.]*) # $4 - heredoc terminator
(\3 # $5 - possible right quote
(?-s:.*\n)) # and rest of the line
(.*?\n) # $6 - Heredoc content
(?<!\n[0-9a-fA-F]{40}\n) # Not another digest
(\4\n) # $7 - Heredoc terminating line
)
/xsm;
my $re_pod = qr/
(?:
(?-s:^=(?!cut\b)\w+.*\n) # Pod starter line
.*? # Pod lines
(?:(?-s:^=cut\b.*\n)|\z) # Pod terminator
)
/xsm;
my $re_comment = qr/
(?:
(?m-s:^[^\S\n]*\#.*\n)+ # one or more comment lines
)
/xsm;
my $re_data = qr/
(?:
^(?:__END__|__DATA__)\n # DATA starter line
.* # Rest of lines
)
/xsm;
# Fold each heredoc, pod section, comment block and data section, each
# into a single line containing a digest of the original content.
#
# This makes further dividing of Perl code less troublesome.
sub pmc_fold_blocks {
my ($class, $source) = @_;
$source =~ s/(~{3,})/$1~/g;
$source =~ s/(^'{3,})/$1'/gm;
$source =~ s/(^`{3,})/$1`/gm;
$source =~ s/(^={3,})/$1=/gm;
while (1) {
no warnings;
$source =~ s/
(
$re_pod |
$re_comment |
$re_here |
$re_data
)
/
my $result = $1;
$result =~ m{\A($re_data)} ? $class->pmc_fold_data() :
$result =~ m{\A($re_pod)} ? $class->pmc_fold_pod() :
$result =~ m{\A($re_comment)} ? $class->pmc_fold_comment() :
$result =~ m{\A($re_here)} ? $class->pmc_fold_here() :
die "'$result' didn't match '$re_comment'";
/ex or last;
}
$source =~ s/(?<!~)~~~(?!~)/<</g;
$source =~ s/^'''(?!') /__DATA__\n/gm;
$source =~ s/^```(?!`)/#/gm;
$source =~ s/^===(?!=)/=/gm;
$source =~ s/^(={3,})=/$1/gm;
$source =~ s/^('{3,})'/$1/gm;
$source =~ s/^(`{3,})`/$1/gm;
$source =~ s/(~{3,})~/$1/g;
return $source;
}
sub pmc_unfold_blocks {
my ($class, $source) = @_;
$source =~ s/
(
^__DATA__\n[0-9a-fA-F]{40}\n
|
^=pod\s[0-9a-fA-F]{40}\n=cut\n
)
/
my $match = $1;
$match =~ s!.*?([0-9a-fA-F]{40}).*!$1!s or die;
$digest_map->{$match}
/xmeg;
return $source;
}
# Fold a heredoc's content but don't fold other heredocs from the
# same line.
sub pmc_fold_here {
my $class = shift;
my $result = "$2~~~$3$4$5";
my $preface = '';
my $text = $6;
my $stop = $7;
while (1) {
if ($text =~ s!^(([0-9a-fA-F]{40})\n.*\n)!!) {
if (defined $digest_map->{$2}) {
$preface .= $1;
next;
}
else {
$text = $1 . $text;
last;
}
}
last;
}
my $digest = $class->pmc_fold($text);
$result = "$result$preface$digest\n$stop";
$result;
}
sub pmc_fold_pod {
my $class = shift;
my $text = $1;
my $digest = $class->pmc_fold($text);
return qq{===pod $digest\n===cut\n};
}
sub pmc_fold_comment {
my $class = shift;
my $text = $1;
my $digest = $class->pmc_fold($text);
return qq{``` $digest\n};
}
sub pmc_fold_data {
my $class = shift;
my $text = $1;
my $digest = $class->pmc_fold($text);
return qq{''' $digest\n};
}
# Fold a piece of code into a unique string.
sub pmc_fold {
require Digest::SHA;
my ($class, $text) = @_;
my $digest = Digest::SHA::sha1_hex($text);
$digest_map->{$digest} = $text;
return $digest;
}
# Expand folded code into original content.
sub pmc_unfold {
my ($class, $digest) = @_;
return $digest_map->{$digest};
}
1;
|