For fun, I put together a script that fakes out all of the libraries that are used by creating a fake library directory with mock lib files. The script is a wrapper for perl -c which hides the fact that the real includes are unavailable. It uses the magical PPI to seek out the includes from the target script.
syntax_check script:
#!/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`;
Script we're going to run perl -c against:
use strict; use warnings; use Data::Dumper; use Test::One; use Multiple::Levels::Two; print "Compiled and ready...\n";
Run it:
spek@scelia ~/scratch $ ./syntax_check script.pl Skipping assumed pragma 'strict' Skipping assumed pragma 'warnings' Skipping available module 'Data::Dumper' script.pl syntax OK
Running the script without the wrapper:
spek@scelia ~/scratch $ perl -c script.pl Can't locate Test/One.pm in @INC (you may need to install the Test::On +e 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/pe +rl-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.
I have no idea how reliable it will be in practice, but it may be worth a shot. I was careless in variable naming and such, as I'm very limited in time right now.
In reply to Re: Perl syntax checking without `perl -c`
by stevieb
in thread Perl syntax checking without `perl -c`
by kcott
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |