I've been trying to improve my understanding of unicode, and wanted to make a script to take a unicode character as $ARGV[0] and search thru all directories and files, looking for that character in either the filename itself, or it's file contents.

I know there are various unicode perlrun options, but I've found I didn't need it. My .bashrc is set to use en_US.UTF-8.

I'm using Perl 14.1, and this seems to work well. If any unicode experts have a way to simplify the code, especially to avoid using decode(), please speak up.

Just run the script with a string containing a unicode character, like ./script 本 and it will recursively search and report all directories or filenames with that string in it's name, and also search thru every file regardless of name for the string in it's content.

#!/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);

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

In reply to UTF-8 and File::Find by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.