Boost your Perl productivity with auto-compile checking

The Perl command line option -c causes Perl to check the syntax of the program, but not execute it (apart from code in BEGIN, CHECK and UNITCHECK blocks - watch out for those). For example:

$ perl -c lib/Devel/DidYouMean.pm
lib/Devel/DidYouMean.pm syntax OK

This is useful but it’s kind of clunky to type it every time you want to check the syntax of a program or file.

Continuous syntax checking

One of my favourite features when developing Catalyst web apps is using the test server to automatically check the syntax of the web app as I develop it. This saves me time as I know immediately if the web app compiles or not and don’t waste time opening up a browser only to get an error. if you’re working on a Unix-based operating system you can achieve a similar effect for any Perl program (not just web apps). The watch program can automatically run the check command. Just start a new terminal, and enter:

$ watch 'perl -c lib/Devel/DidYouMean.pm'

Giving this output:

Every 2s perl -c lib/Devel/DidYouMean.pm           Sat Nov  8 2014

lib/Devel/DidYouMean.pm syntax OK

In this case I’m watching the file lib/Devel/DidYouMean.pm but you can provide any path to a Perl file that you want to check for syntax errors. By default watch will run the command every 2 seconds. So if I save a bad update to the file, the watching terminal window will show the error:

Every 2.0s: perl -c lib/Devel/DidYouMean.pm           Sat Nov  8 2014

syntax error at lib/Devel/DidYouMean.pm line 122, near "} keys"

lib/Devel/DidYouMean.pm had compilation errors.

This enables me to catch the error before running the program, saving time.

Checking syntax in a text-editor

Using watch is useful, but I find it can be annoying to have to check a separate terminal window to know if my program compiles or not. Another way to do this is to run the command from within your text-editor. I’ll show how you how to do this in vim, but it should be possible to do this in any text-editor that has save events which you can hook in to (e.g. examples for Sublime Text and Emacs).

Add the following line to your .vimrc file:

autocmd BufWritePost *.pm,*.t,*.pl echom system("perl -Ilib -c " . '"' . expand("%:p"). '"' )

What this command does is every time a file ending in .pm, .t, or.pl is saved, vim will run the check syntax command on the file, echoing the results to the current window. Reload your .vimrc with this vim command: :so $MYVIMRC.

Now you don’t have to bother setting up a separate terminal window and watching the file; vim will notify you immediately if any Perl file is saved with compilation errors. Much more convenient!

Alternative Methods in Vim

Several readers got in touch to recommend the Syntastic plugin for Vim (manual). One nice thing about Syntastic is you can chain compile checks: first run perl -c, if it passes, then run Perl::Critic and so on. Syntastic also integrates syntax checkers for many other languages, so if Vim is your editor of choice, you might want to check it out.

A simpler alternative to Syntastic is to use Vim’s built-in compiler support. With a Perl file in the current buffer, type:

:compiler perl
:make
:cope

This will run Perl’s syntax checks checks on the current buffer. Vim reads the output into an error list, which the :cope command displays. You can jump to the line referenced by a specific error by pressing the enter key (manual).

Updates: BEGIN, CHECK, UNITCHECK blocks caution added. Emacs link and addition Vim methods added. 2014-11-10

Vim autocmd example updated to handle filepaths containing spaces. Thanks to Henry An for the suggestion. 2015-01-22

Cover image © Alan Kotok image has been digitally altered


This article was originally posted on PerlTricks.com.

Tags

David Farrell

David is the editor of Perl.com. An organizer of the New York Perl Meetup, he works for ZipRecruiter as a software developer, and sometimes tweets about Perl and Open Source.

Browse their articles

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub