in reply to Bulk check for successful compilation
Aw, neat... this is a new one for me -- how to modify a subroutine in a non-object-oriented CPAN module in a way that doesn't involve monkeying with its actual source code.
I realized that the Test::Compile::Internal CPAN module doesn't consider ".cgi" files to be Perl "scripts" -- only .pl files, .psgi files, and files that don't have any extension but include the string "perl" in their shebang lines. But I want it to test my .cgi files also (all of which run perl via their shebang lines rather than python or another executable).
Using brian d. foy's Mastering Perl technique (2d ed. at page 176), I did this, and it worked the first time. This won't blow most PerlMonks readers away, but it's the first time I've used the technique (rather than copying the entire CPAN module's distribution, renaming it Local::Test::Compile::Internal, modifying its source code, etc.).
Original script, which doesn't check .cgi files (only checks .pl, .pm, .psgi and certain no-extension files, due to the way Test::Compile::Internal works):
#!/opt/perl524 use strict; use warnings; use Test::Compile::Internal; my $test = Test::Compile::Internal->new(); $test->all_files_ok( '/www/cgi-bin' ); $test->done_testing();
New script, which also checks .cgi files:
#!/opt/perl524 use strict; use warnings; BEGIN { use Test::Compile::Internal; no warnings 'redefine'; *Test::Compile::Internal::all_pl_files = sub { my ( $self, @dirs ) = @_; @dirs = @dirs ? @dirs : _pl_starting_points(); my @pl; for my $file ( $self->_find_files(@dirs) ) { if ( $file =~ /(\.pl|\.psgi|\.cgi)$/i ) # <== Here's my m +odification. { # Files with .pl or .psgi or .cgi extensions are perl +scripts push @pl, $file; } elsif ( $file =~ /(?:^[^.]+$)/ ) { # Files with no extension, but a perl shebang are perl + scripts my $shebang = $self->_read_shebang($file); if ( $shebang =~ m/perl/ ) { push @pl, $file; } } } return @pl; } } ## end BEGIN my $test = Test::Compile::Internal->new(); $test->all_files_ok( '/www/cgi-bin' ); $test->done_testing();
Here is the subroutine's code in the Test::Compile::Internal module that's effectively overridden by the BEGIN code in my script, above.
sub all_pl_files { my ($self, @dirs) = @_; @dirs = @dirs ? @dirs : _pl_starting_points(); my @pl; for my $file ( $self->_find_files(@dirs) ) { if ( $file =~ /\.p(?:l|sgi)$/i ) { # Files with .pl or .psgi extensions are perl scripts push @pl, $file; } elsif ( $file =~ /(?:^[^.]+$)/ ) { # Files with no extension, but a perl shebang are perl scr +ipts my $shebang = $self->_read_shebang($file); if ( $shebang =~ m/perl/ ) { push @pl, $file; } } } return @pl; }
|
|---|