Skip to Content
Perl Hacks
book

Perl Hacks

by Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe
May 2006
Beginner
298 pages
6h 51m
English
O'Reilly Media, Inc.
Content preview from Perl Hacks

Hack #63. Run Tests Automatically

See what's broken right after you break it!

The idea behind test-driven development is that rapid feedback based on comprehensive test coverage helps you write sane, clean code that never regresses. Once you know how to write tests, the next step is getting sufficient feedback as soon as possible.

What if you could run your tests immediately after you write a file? That's fast feedback that can help you see if you've broken anything. Best of all, you don't have to switch from your most beloved editor!

The Hack

The heart of this hack is the onchange program:

#!/usr/bin/perl

# onchange file1 file2 ... fileN command

use strict;
use warnings;

use File::Find;
use Digest::MD5;

my $Command     = pop @ARGV;
my $Files       = [@ARGV];
my $Last_digest = '';

sub has_changed
{
    my $files = shift;
    my $ctx   = Digest::MD5->new( );

    find( sub { $ctx->add( $File::Find::name, ( stat($_) )[9] ) },
        grep { -e $_ } @$files );

    my $digest      = $ctx->digest( );
    my $has_changed = $digest ne $Last_digest;
    $Last_digest    = $digest;

    return $has_changed;
}

while (1)
{
    system( $Command ) if has_changed( $Files );
    sleep 1;
}

This takes a list of files or directories to monitor and a command to run when they change. Of course, using File::Find means that this processes directories recursively.

Running the Hack

For a Perl application that uses the standard CPAN module structure (modules in lib/, tests in t/, and either a Makefile.PL or Build.PL to control everything), running is easy. Open a new terminal, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Debugged

Perl Debugged

Peter Scott, Ed Wright
Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Learning Perl 6

Learning Perl 6

brian d foy
Perl by Example

Perl by Example

Ellie Quigley

Publisher Resources

ISBN: 0596526741Supplemental ContentErrata Page