Vim: Does Your Pod Have Valid Perl?

Ovid on 2009-11-14T10:25:27

After writing some vim code to run your POD code, I realized I needed another snippet to simply tell me if my POD code compiled (without running it). The vim mapping:

vnoremap  c :!perl ~/bin/validperl

Select a region of Perl code, hit ",c" (or whatever your leader is) and if no compilation errors are detected, you'll see no change. Otherwise, they will be added after an "__END__" token. Here's the code for validperl:

#!/usr/bin/env perl 

use strict;
use warnings;
use File::Temp 'tempfile';

my $tmpdir = '/var/tmp';

my ( $fh, $snippet ) = tempfile(
    'eval_XXXX',
    SUFFIX => '.pl',
    DIR    => $tmpdir,
); 
my $code = do { local $/;  };
print $fh $code or die "Could not print code to ($snippet): $!";
close $fh or die "Could not close ($snippet): $!";

my $perl = $^X;
print $code; 
my $output = qx{ $perl -Ilib -c $snippet 2>&1 };
exit if $output =~ m{^$tmpdir/eval_\w+.pl syntax OK$};
$output =~ s/\n/\n /g;
print " __END__\n $output";


Incomplete examples

bart on 2009-11-14T18:54:45

This is not working with incomplete sample code, is it? Code that does not include the necessary (but trivial) use Foo::Bar lines, or object instantiation (Foo::Bar->new)?

Just because code is incomplete, doesn't mean that it's wrong.

And adding redundant code is not making examples clearer.

Re:Incomplete examples

Ovid on 2009-11-14T20:22:45

It's certainly not perfect for all things and that's unfortunate. I don't know of any other way around that. I've thought of various tricks (s/use/require/), but that breaks other things. In short, it won't work for everyone, but if it does, it's a nice check.