#!/usr/bin/perl use warnings; use strict; use File::Find; use Encode qw(decode); my $path = '.'; # use utf8::all or use the commented lines beneath use utf8::all; # use this instead of the utf8::all module # make input strings utf8 #$_ = Encode::decode('UTF-8', $_) for @ARGV; # make STDIN STDOUT STDERR to be utf8, #binmode STDOUT, ':encoding(UTF-8)'; #binmode STDIN, ':encoding(UTF-8)'; #binmode STDERR, ':encoding(UTF-8)'; my $search_str = $ARGV[0] or die "need a search string\n $!\n"; print "search-> $search_str\n"; my $regex = qr/\Q$search_str\E/; print "regex-> $regex\n"; # finds all files with $regex in it's filename # and all files who have a line matching $regex find (sub { # make File::Find unicode $_ = decode('utf8',$_); $File::Find::dir = decode('utf8', $File::Find::dir); if ($_ =~ /$regex/){ # check for name match if( -d ){ print "dirname match: $File::Find::dir/$_\n" }else{ print "filename match: $File::Find::dir/$_\n" } } # now check filename's contents my $filename = $_; # avoid $_ confusion # the following line is not needed with utf8::all, # otherwise use the following #open (my $fh,'<:encoding(UTF-8)', $filename); open (my $fh,'<', $filename) or warn "Can't open $filename $!\n"; while(<$fh>){ if ($_ =~ /$regex/){ print "text match in $File::Find::dir/$filename: $_\n"; } } }, $path);