Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl syntax checking without `perl -c`

by stevieb (Canon)
on Dec 05, 2020 at 18:26 UTC ( [id://11124719]=note: print w/replies, xml ) Need Help??


in reply to Perl syntax checking without `perl -c`

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found