Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: grep and find file weirdness

by blazar (Canon)
on Jun 20, 2007 at 19:40 UTC ( [id://622373]=note: print w/replies, xml ) Need Help??


in reply to grep and find file weirdness

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__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://622373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-26 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found