in reply to Grep a word and print the filename

Take a look at this node Why I hate File::Find and how I (hope I) fixed it and try this

#!/usr/bin/perl use File::Find; use strict; my $country; sub getCountry{ my $locn = shift; my $dir ='c:/temp/countries/'; find(sub{ txtsearch($locn) }, $dir); } sub txtsearch { my $locn = shift; if (-f $_) { open my $fh, "<", $_ or do { warn qq(Unable to open "$File::Find::name": $!); #checks permiss +ions return; }; while(<$fh>) { if (/\Q$locn\E/) { my $country = (split '/', $File::Find::name)[-1]; print "$locn found in $country\n"; last ; #stops searching once found } } } }; getCountry("NY"); getCountry("London");
poj

Replies are listed 'Best First'.
Re^2: Grep a word and print the filename
by alokranjan (Acolyte) on Aug 15, 2015 at 21:55 UTC
    Thanks...