in reply to in place editing a list of files

Your code works fine for me, with the slight change of chomping $array at the beginning of the outer loop.

But I'm not sure why you don't just put all the filenames in @ARGV:

#!/usr/bin/perl use strict; use warnings; #edit contains the complete path of the files to be edited my $path = "./edit" ; #geting a file handler open(FL, "$path"); #getting all the file names in the @ARGV so that i can process them chomp(@ARGV = <FL>); # $INPUT_RECORD_SEPARATOR $/=""; # $INPLACE_EDIT $^I=".bak"; while(<>) { s/CONFIDENTIAL(.*?)own\s+risk/ /sm; print ; }
It's possible you are having file permission problems, in which case, you are probably better off not relying on $^I but instead explicitly renaming each file and opening the old and new names, checking each operation for success and giving a meaningful error message on failure.

Replies are listed 'Best First'.
Re^2: in place editing a list of files
by liverpole (Monsignor) on Jan 07, 2007 at 18:59 UTC
    ysth, you're correct, that it works fine if no errors occur.

    But as soon as the file "./edit" doesn't exist, or doesn't have the correct permissions, you'll get a cryptic:

    readline() on closed filehandle FL at C:\Documents and Settings\liverp +ole\edit.pl line 13.

    or something similar.  What's even worse is that, at least on my Windows box, the program doesn't terminate, forcing you to ^C to get out of it.

    That's why I was suggesting the error-checking when opening files.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re^2: in place editing a list of files
by rjsaulakh (Beadle) on Jan 08, 2007 at 18:05 UTC

    the script run into endless loop once we enter the while loop and no substitution is being done

      I can only suggest you run it under the debugger, examining all variables as you go and seeing where things go differently than you expect. Alternatively, rewrite to not use the empty <> operator and explicitly do every open, rename, print, and close, checking for errors after each one.