in reply to foreach not working as I expected
It's not possible to open more than one file per filehandle - hence the handle is getting reassigned each time the loop iterates. This is why you get output in one file but not the others.
It's also good practice to close each file when you're done appending, even though the file will be closed when the filehandle goes out of scope.
A quick rewrite of your loop gives:
foreach my $file (<*.txt>) { open ADDTO, $file or die "Couldn't open $file for append.\n"; print ADDTO $txt; close ADDTO; }
This should give you what you expect.
Hope that helps ..
-- Foxcub
Update: Added "or die" to the open.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: foreach not working as I expected
by Jenda (Abbot) on Dec 18, 2002 at 22:18 UTC |