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; }

In reply to Re: Bulk check for successful compilation by davebaker
in thread Bulk check for successful compilation by davebaker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.