#!/usr/local/bin/nperl 

use File::Find;
use Getopt::Std;

my %opt;
getopts("mlc",\%opt);

$expr = shift;

print "Matching '$expr'\n";

sub match
{
 if (/$expr/o)
  {
   if ($opt{'l'})
    {
     print "$File::Find::name\n";
     return 1;
    }
   $count++;
   unless ($opt{'c'})
    {
     print "$File::Find::name:$.: $_" 
    }
  }
 return 0;
}

sub wanted
{
 $File::Find::prune = 0;
 if (-T $_ && !/%$/)
  {
   local $file   = ($_);
   local ($_);
   open($file,"<$file") || die "Cannot open $file:$!";
   while (<$file>)
    {
     last if &match;
    }
   close($file);
   if ($opt{'c'} && $count)
    {
     print "$File::Find::name: $count\n" 
    }
  }
 elsif (-d $_)
  {
   $File::Find::prune = 1 if ($_ eq 'mTk' && $opt{'m'});
  }
}

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

find(\&wanted,@ARGV);
