Anywho, here's the distilled example...

In addition to the other good suggestions you got (I hope not to repeat too many of them!) here are a few others:

my @files = ();

No need for the initialization and it doesn't add to clarity IMHO.

find(sub { $File::Find::name =~ m/${directory}(.*)$/; push @files, $1} +, $directory); @files = grep { is_dos_format("$directory/$_") } @files;

Rather than collecting filenames in @files to process the latter with grep later, why don't you do so in the first place as files are being searched? If the files are many, then your program should be more responsive. Also, I don't understand the logic of stripping the base directory only to reinsert it later...

foreach my $file (@files) { print "FAILED FILE - $file\n"; }

Oh, and why another loop too?!? (grep is one in disguise: you're doing it twice when there's no logical reason to.)

if ($_ =~ m/\r\n/) {

The whole point of the $_ pronoun is that it is the topicalizer: you either use an explicit variable name and the binding operator or just the match.

All in all I believe your code may be rewritten like:

#!/usr/bin/perl use strict; use warnings; use File::Find; my $directory = '/tmp/rja/find_test'; find { no_chdir => 1, wanted => sub { return unless -f; open my $fh, $_ or die "Can't open `$_': $!\n"; my $file=$_; /\r\n/ and print "FAILED FILE - $file\n" and return while <$fh>; } }, $directory; __END__

In reply to Re: grep and find file weirdness by blazar
in thread grep and find file weirdness by reasonablekeith

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.