in reply to Fastest way to minimally check that file contains perl code?
G'day DRVTiny,
Welcome to the Monastery.
"When i using perl -c it takes too much time to check ..."
As I came to post this, I saw that ++vr had posted a Win32 solution. Our code is functionally similar up to the point of getting the perl -c exit code; then our methods diverge somewhat. Our general thinking about the problem was also very similar: don't let perl -c run for as long as it likes; let it run for as long as you like.
Here's my code.
#!/usr/bin/env perl use strict; use warnings; use constant { TIMEOUT_USECS => 100000, MIN_LINES_TO_ASSESS => 10, OUT_FILE => 'pm_11114214_minimal_is_perl_test.out', }; use constant CMD_LINE => 'perl -c IN_FILE 2> ' . OUT_FILE; use Time::HiRes 'ualarm'; for my $file (@ARGV) { my $cmd = CMD_LINE; $cmd =~ s/IN_FILE/$file/; eval { local $SIG{ALRM} = sub { die }; ualarm TIMEOUT_USECS; `$cmd`; $? and die; ualarm 0; print "$file is valid Perl code.\n"; 1; } or do { ualarm 0; heuristic_check($file); }; } sub heuristic_check { my ($file) = @_; if (-z OUT_FILE) { print "$file could be Perl code.\n"; } else { my $file_lines = (split ' ', `wc -l $file`)[0]; my $out_lines = (split ' ', `wc -l @{[OUT_FILE]}`)[0]; if ($file_lines < MIN_LINES_TO_ASSESS) { print "$file is too small to assess. [$file_lines lines]\n +"; } elsif ($out_lines > $file_lines) { print "$file does not look like Perl code.\n"; } else { printf "%s has a %.02f%% chance of being Perl code\n", $file, 100 * ($file_lines - $out_lines) / $file_lines; } } return; }
With the timeout set to 1ms, it assessed itself as "a 98.18% chance of being Perl code"; at 10ms it was a tad more confident with "a 98.25% chance of being Perl code"; and at 100ms, it was sure, with "is valid Perl code".
I tested a tiny text file I had in my tmp directory. It wasn't Perl and I decided tiny files were too small to assess if they didn't pass perl -c. In that case, the output showed "too small to assess. [6 lines]". However, I did create a file with just print "Hello, world!\n";. That gave "hello.pl is valid Perl code." at 100ms but, at 10ms, the output was "hello.pl is too small to assess. [1 lines]".
And I tested a plain text file containing no Perl code: that gave "does not look like Perl code" at 1ms, 10ms and 100ms.
— Ken
|
|---|