On Sep 16, Johnson, Shaunn said:

>* gets a list of files
>* opens the files with a 'foreach $f(@list)'
>* does a 'while <> { ... } close (FILE)'
>
>The script works, but if I run it, it loops
>continually and edits (then re-edits) the files.
>I think I could use a lock file to do until I
>get a particular file, but I'm not sure how
>to go about it - at the same time, it may not be
>the more productive way to go.

Your code is doing too much work.

>my @list = grep {/\.txt/ } readdir(DIR) or die "Can not read the dir\n";

This creates the list of files.

>foreach $file(@list) {
>open (FILE, $file) or die "can not open this file: $!";
>
>local $^I=".bak";       # to keep a backup of the files
>                        # set to "" if i don't want backups
>local @ARGV = @list;    # the files to work on

Perhaps you mean

  local @ARGV = $file;

instead.

>        while (<>) {
>                s!$pattern!$new_ptrn!g ;
>                print;
>                } # end while loop
>        close (FILE);
>} #end of for loop

Better yet, remove the for loop.  I'll show the code in a moment.

>close (DIR);

That should be closedir().

You can do this without opendir(), and without grep() or the for loop.

  {
    local $^I = ".bak";
    local @ARGV = glob "$directory/*.txt";
    while (<>) {
      s/foo/bar/g;
      print;
    }
  }

Ta da!

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to