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

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.