reasonablekeith has asked for the wisdom of the Perl Monks concerning the following question:
I have a simple script which should list all the files in a given directory, then read them one by one, checking for dos format. That list is then printed out. My problem is, after the grep, the @files array ends up containing lines from the files I'm reading, when it originally contains the paths to the files.
I'm guessing this is because (inside the grep )$_ is an alais to the array value, so the file read is automagically smashing this, and making it back into my array.
That would be a pretty bad gotcha, and one I'd be surprised I'd not run across until now. If that's is indeed the case, could someone please suggest an alternative/ways to avoid this?
Anywho, here's the distilled example...
NB: This has a byte size on my PC of 666, make of that what you will :S#!/usr/bin/perl use strict; use File::Find; my $directory = "/tmp/rja/find_test"; my @files = (); find(sub { $File::Find::name =~ m/${directory}(.*)$/; push @files, $1} +, $directory); @files = grep { is_dos_format("$directory/$_") } @files; foreach my $file (@files) { print "FAILED FILE - $file\n"; } #============================================================== sub is_dos_format { my $file_path = shift; open RJA, $file_path or die("Couldn't open file for reading ($file +_path): $!"); while (<RJA>) { if ($_ =~ m/\r\n/) { return 1; } } return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: grep and find file weirdness
by japhy (Canon) on Jun 20, 2007 at 15:58 UTC | |
|
Re: grep and find file weirdness
by blazar (Canon) on Jun 20, 2007 at 19:40 UTC | |
|
Re: grep and find file weirdness
by EvanCarroll (Chaplain) on Jun 20, 2007 at 16:19 UTC | |
by blazar (Canon) on Jun 20, 2007 at 19:45 UTC | |
|
Re: grep and find file weirdness
by graff (Chancellor) on Jun 21, 2007 at 04:23 UTC |