#!/usr/bin/env perl
use warnings;
use strict;
use feature 'say';
use File::Path qw(make_path);
use PPI;
if (! @ARGV) {
say "USAGE: ./syntax_check perl_script.pl";
exit;
}
my $file = $ARGV[0];
die "Can't open file '$file': $!" if ! -f $file;
my $lib_dir = 'test_lib/';
if (! -d $lib_dir) {
mkdir $lib_dir or die $!;
}
my $doc = PPI::Document->new($file);
my $includes = $doc->find('PPI::Statement::Include');
for my $include (@$includes) {
my $module = $include->module;
my $package = $module;
# Skip pragmas
if ($module eq lc $module) {
say "Skipping assumed pragma '$module'";
next;
}
$module =~ s|::|/|g;
if (my ($dir, $file) = $module =~ m|^(.*)/(.*)$|) {
$file .= '.pm';
my $path = "$dir/$file";
# Skip includes that are actually available
if (exists $INC{$path}) {
say "Skipping available module '$package'";
next;
}
if (! -d "$lib_dir/$dir") {
make_path("$lib_dir/$dir") or die $!;
}
if (! -f "$lib_dir/$path") {
open my $wfh, '>', "$lib_dir/$path" or die $!;
print $wfh '1;';
close $wfh or die $!;
}
}
}
say '';
`perl -I$lib_dir -c $file`;
####
use strict;
use warnings;
use Data::Dumper;
use Test::One;
use Multiple::Levels::Two;
print "Compiled and ready...\n";
##
##
spek@scelia ~/scratch $ ./syntax_check script.pl
Skipping assumed pragma 'strict'
Skipping assumed pragma 'warnings'
Skipping available module 'Data::Dumper'
script.pl syntax OK
##
##
spek@scelia ~/scratch $ perl -c script.pl
Can't locate Test/One.pm in @INC (you may need to install the Test::One module) (@INC contains: /home/spek/perl5/perlbrew/perls/perl-5.26.1/lib/site_perl/5.26.1/x86_64-linux /home/spek/perl5/perlbrew/perls/perl-5.26.1/lib/site_perl/5.26.1 /home/spek/perl5/perlbrew/perls/perl-5.26.1/lib/5.26.1/x86_64-linux /home/spek/perl5/perlbrew/perls/perl-5.26.1/lib/5.26.1) at script.pl line 5.
BEGIN failed--compilation aborted at script.pl line 5.