#!/usr/bin/perl

# announceGremlins
# This script is intended for checking text files for the presence of gremlins.
# (A "gremlin" is the BBEdit name for an unwanted character, often invisible,
#  that has somehow gotten into your text file.)
# The current implementation of this script looks for characters that
# are not in the POSIX printable character class. 
# You can supply filenames as command-line arguments, or send the text you
# want checked via STDIN.
# Cameron Hayne (macdev@hayne.net)  February 2008

use strict;
use warnings;

while (<>)
{
    chomp;
    my $line = $_;
    while ($line =~ /[[:^print:]]/g)
    {
        my $colNum = pos($line);  # we want a 1-based column number
        my $before = $`;
        my $after = $';
        print "Non-printable character at column $colNum of line $. of file $ARGV\n";
        print "$before?$after\n";
    }
}
continue
{
    # reset line numbering on each input file
    close ARGV if eof; # Not eof()! (eof with parentheses is different)
}

