perl-c-ontinuos

TL;DR

A little shell script to keep a Perl program under check.

When I’m working on a Perl program, every now and then I do some basic check to see if it compiles and its syntax is OK:

$ perl -c some-program.pl
some-program.pl syntax OK

That’s because… well, sometimes a syntax error actually gets in:

$ perl -c some-other-program.pl
Global symbol "$whatever" requires explicit package name...
...
BEGIN not safe after errors--compilation aborted ...

It can be useful to keep a program under continuous check, in the sense that I want to figure out quickly when it breaks the compilation. The following script does exactly this in a Linux machine with inotifywait (part of inotify) installed:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/bin/sh

main () {
   local PROGRAM=$(readlink -f "$1")
   local COMMAND=$2

   [ -n "$PROGRAM" ] || die 'no program to watch provided'
   [ -n "$COMMAND" ] || COMMAND='perl -c'

   _check

   local DIRNAME="$(dirname "$PROGRAM")"
   local BPROGRAM="$(basename "$PROGRAM")"
   inotifywait -q -e close_write,moved_to,create -m "$DIRNAME" \
      | while read FILENAME EVENT ; do
         if [ "CLOSE_WRITE,CLOSE $BPROGRAM" = "$EVENT" ] ; then
            _check
         fi
      done
}

die() { printf >&2 '%s\n' "$*" ; exit 1 ; }
_check() {
   printf  '\n'
   $COMMAND "$PROGRAM"
   printf  '%s\n' '-------------------'
}

main "$@"

Local version here.

Use it by passing the name of the Perl program you want to monitor:

$ perl-c-ontinuous whatever.pl 

/path/to/whatever.pl syntax OK
-------------------

syntax error at /path/to/whatever.pl line 8, near "my "
Global symbol "$original" requires explicit package name ...
...
BEGIN not safe after errors--compilation aborted ...
-------------------

/path/to/whatever.pl syntax OK
-------------------

At each save, the perl -c command is executed, and in case of syntax errors… an error message is printed. Thanks to inotify this can happen only upon saving the file, sparing system resources.


Comments? Octodon, , GitHub, Reddit, or drop me a line!