Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I guess the answer will end up being 'obvious', but I'm banging my head against the wall here...

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...

#!/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; }
NB: This has a byte size on my PC of 666, make of that what you will :S
---
my name's not Keith, and I'm not reasonable.

In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-18 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found