Shaunn Johnson wrote:
> 
> Howdy:

Hello,

> I'm trying to find the best solution for breaking
> out of a loop when editing a list of files.
> 
> I have a script that:
> 
> * 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.
> 
> [--snip script--]
> my @list = grep {/\.txt/ } readdir(DIR) or die "Can not read the dir\n";
> 
> # create a loop to search for instances of
> # control characters and change it to something else
> 
> 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
>         while (<>) {

You are looping through all the files in @list using foreach above and
then you are looping through all the files in @list using while (<>). 
This means that you are modifying each file the number of times for each
file in the directory.  Are you sure this even works at all because you
are not prepending the directory name to the file name?


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


Something like this should work:

my $dir = '/home/someone';
local $^I = '.bak';     # to keep a backup of the files
                        # set to "" if i don't want backups

opendir DIR, $dir or die "Cannot open $dir: $!";
local @ARGV = map "$dir/$_", grep /\.txt$/, readdir DIR;
close DIR;

# create a loop to search for instances of
# control characters and change it to something else

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

__END__



John
-- 
use Perl;
program
fulfillment

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

Reply via email to