Reputation: 5927
My perl script below
@array = qw(one two three four five);
sleep (60);
foreach (@array){
open(new,">>$_.txt");
print new "$_ This is testing\n";
}
sleep(120);
open (new2,">>for2.txt");
print new2 "Hai";
In my script. New files are open by the foreach condition. In my script open five files. But my problem is First open the new five files but not writted in the last array element of the opening file. When finished the outer of the condition then only writted into the foreach last element file. For example: New files are one two three four five are created and $_ this is testing are writted into file except the file five when file for2 created and writted the value into the file then the file five is involve to write. How to change it.?
Upvotes: 1
Views: 134
Reputation: 35198
Your filehandles to files one
through four
are closed and flushed when you open a subsequent filehandle of the same name. However, the handle for the last file is left open until the end of the script.
One way to to fix this is to explicity close
your file handles so they are flushed.
foreach (@array){
open(new,">>$_.txt");
print new "$_ This is testing\n";
close new;
}
The better solution though is to use more Modern Perl techniques. If you use lexical filehandles, they will be automatically closed when they go out of scope.
use strict;
use warnings;
use autodie;
my @array = qw(one two three four five);
sleep(60);
for (@array) {
open my $fh, '>>', "$_.txt";
print $fh "$_ This is testing\n";
}
sleep(120);
open my $fh, '>>', "for2.txt";
print $fh "Hai";
Upvotes: 4
Reputation: 126722
Output to a file handle is buffered, and will normally be flushed only when the buffer is full or when the file handle is closed. Perl closes file handles implicitly if you open another file on the same handle, or when the program terminates.
The solution is to set your file handles to autoflush as shown here. If you are running on an older version of Perl, before version 14 of Perl 5, then you will also need to add use IO::Handle;
to the top of your program.
foreach (@array){
open(new,">>$_.txt");
new->autoflush;
print new "$_ This is testing\n";
}
Update
There are a few things that you should take note of that would improve your Perl programming enormously
You must always use strict
and use warnings
at the top of every Perl program you write, and declare all your variables as late as possible using my
You should use the three-parameter form of open
with lexical file handles, and you must always check that an open
call succeeded, otherwise subsequent reads and writes will fail and there is little point in continuing
You should use copious amounts of whitespace to lay out your code better and make it more readable
I would write the code in your question like this
use strict;
use warnings;
my @files = qw( one two three four five );
sleep(60);
for my $name ( @files ){
my $file = "$name.txt";
open my ($new_fh), '>>', $file or die "Unable to open '$file' for appending: $!";
$new_fh->autoflush;
print $new_fh "$name This is testing\n";
}
sleep(120);
open my ($fh_new2), '>>', 'for2.txt' or die "Unable to open 'for2.txt' for appending: $!";
print $fh_new2 'Hai';
Upvotes: 5