The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/bin/sh
exec perl -wSx $0 "$@"
#!perl -w

use Getopt::Std;

my %opt;

use File::Find;

getopts("Flce:",\%opt);

my $expr;
unless ($opt{'F'})
 {
  $expr = (defined $opt{'e'}) ? $opt{'e'} : shift;
  $expr =~ s/#/\\#/g;
  $expr = qr#$expr#;
  warn "Matching '".$expr."'\n";
 }

sub match
{
 if ($unsure)
  {
   return 1 unless (/#!.*perl/);
   $unsure = 0;
  }
 if ($opt{'F'})
  {
   if (/\bfont\b.*(["'])(-[^-]*){2,13}\1/i)
    {
     if ($opt{'l'})
      {
       print "$File::Find::dir/$file\n";
       return 1;
      }
     $count++;
     unless ($opt{'c'})
      {
       print "$File::Find::dir/$file:$.: $_"
      }
    }
  }
 elsif ($_ =~ $expr)
  {
   if ($opt{'l'})
    {
     print "$File::Find::dir/$file\n";
     return 1;
    }
   $count++;
   unless ($opt{'c'} || $opt{'l'})
    {
     print "$File::Find::dir/$file:$.: $_"
    }
  }
 return 0;
}

sub wanted
{
 $File::Find::prune = 0;
 if (-T $_ && !/%$/)
  {
   local $unsure = !/\.p[ml]$/;
   local $file   = ($_);
   local ($_);
   local $count = 0;
   my $fh;
   unless (open($fh,"<$file"))
    {
     warn "Cannot open $file:$!";
     return;
    }
   while (<$fh>)
    {
     last if &match;
    }
   close($fh);
   if ($opt{'c'} && $count)
    {
     print "$File::Find::dir/$file: $count\n"
    }
  }
 elsif (-d $_)
  {
   $File::Find::prune = 1 if ($_ eq 'blib');
  }
}


@ARGV = '.' unless (@ARGV);

find(\&wanted,@ARGV);