Today I was asked to change the expression "origtext" to "changedtext" on every file within a large directory structure. For lack of knowledge of a better tool I decided to use perl and File::Find. My question is about File::Find in general so I'll post the bare skeleton that I wrote to just list the files that are about to be changed (i.e. a grep type script).

What I encountered was my find.pl script (listed below) ran out of memory on the machine when I ran the script on the base directory (containing 395 subdirectories). I ended up writing a small script to do the find.pl script on one sub-directory at a time.

Was the out of memory problem due to a failure in my use of perl, die I miss something in the File::Find pod, or is it due to the way File::Find works on recursion?

This is the script that lists the files I'm going to change, really just a test that I had the RE and other bits right before actually changing files.
find.pl
#!/usr/bin/perl -w use strict; use File::Find; my($find, @directories) = (@ARGV); unless (scalar @directories) { print "Usage: find.pl FINDSTRING DIR1...\n"; exit; } find(\&do_this, @directories); exit; sub do_this { if(! -f $_) { # if it's not a regular file skip it return; } if(open(F, $_)) { undef $/; my $file = <F>; if((defined $file) && ($file ne '') && ($file =~ /\Q$find\E/giso)) { # the if defined was added # later after I realized that an # empty file would leave $file undef! print "$File::Find::name\n"; } close(F); } else { warn "Unable to open '$_': $!"; } }


Just a note in the end the job was accomplished I ask now for learning purposes so next time I can do it better or more correctly.

Thanks for any advice you can give, including general critique,
James

In reply to Is this normal for File::Find or where did I go wrong. by illitrit

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.