##
# Copyright (c) 2008 Health Market Science, Inc
##
package _TMP;
use strict;
use warnings;
our $CVS_ID = '$Id: emacs-utils.el,v 1.82 2008/07/11 13:35:40 kburton Exp $'; #'
our $VERSION = ( qw$Revision: 1.82 $ )[1];

sub run {
  my($self) = @_;

  unless (@ARGV) {
    die "Error, you must supply one or more input files: $0 file1 [file2 ...]\n";
  }


  my($longest,$foundFile) = ('','');

  foreach my $file (@ARGV) {
    my $data = $self->readFile($file);
    my $long = '';
    foreach my $word (split /\b/, $data ) {
      $word =~ s/[^A-Za-z]//msg;
      next unless length $word > 1;
      if ($self->isPal($word) && length $word > length $long) {
        $long = $word;
      }
    }
    print "Longest (", length($long),") in $file => $long\n";
    if ( length $long > length $longest ) {
      $longest = $long;
      $foundFile = $file;
    }
  }
  print "Longest (",length($longest),") over all was: $foundFile => $longest\n";

}


sub openFile {
  my($self,$fname,$mode) = @_;
  $mode ||= '<';
  my $fh;
  unless (open $fh, $mode, $fname) {
    die "Error opening file:$fname mode:$mode : $!\n";
  }
  return $fh;
}

sub readFile {
  my($self,$file) = @_;

  local $/ = undef;
  my $fh = $self->openFile($file);
  my $data = <$fh>;
  return $data;
}

sub reverseString {
  my($self,$str) = @_;
  return join( '', reverse( split //, $str ));
}

sub isPal {
  my($self,$instr) = @_;
  my $str = uc $instr;
  return undef unless length($str) > 1;
  return $str eq $self->reverseString($str);
}



1;

_TMP->run;
