Dear Monks,
I have a perl script that is (re-)writing a file using Tie::File, then calling a process to run in the background with this file as the input. I do this in a loop, so that I get a different version of the file for each runthrough of the process.
The problem is (I think..) that the file has not always finished writing by the time I call the next process. How do I wait for a file to finish writing before calling my process? I do not think I am suffering from buffering. I have $|=1. Do I maybe need to somehow set this for the individual file instead?
Perhaps I have got completely the wrong end of the stick and I have another problem here.. if so, does anyone have any suggestions?
This code should illustrate my problem:
#!/usr/bin/perl
#file: plet_test.pl
use strict;
use warnings;
use Data::Dumper;
$|=1;
&do_something(@ARGV);
exit 0;
sub do_something
{
open(FILE,'<',$_[0]) || die;
my $line=<FILE>;
print "$line\n";
}
#! /usr/bin/perl
#file: call_stuff.pl
use strict;
use warnings;
use Tie::File;
$|=1;
for(my $i=1;$i<=10;$i++){
my @array=();
tie(@array,'Tie::File',"my_file.txt");
$array[1]="this is a line!";
$array[2]="woah, another line";
$array[3]="more lines??";
$array[4]="and so on....";
$array[0]="this is the first line. it is number $i. haha!";
untie(@array);
system('./plet_test.pl my_file.txt &');
}
exit 0;
On my system, this gives the output:
this is the first line. it is number 2. haha!
this is the first line. it is number 4. haha!
this is the first line. it is number 5. haha!
this is the first line. it is number 6. haha!
this is the first line. it is number 7. haha!
this is the first line. it is number 8. haha!
this is the first line. it is number 9. haha!
this is the first line. it is number 10. haha!
this is the first line. it is number 10. haha!
this is the first line. it is number 10. haha!
Which is the same type of problem as I get with my proper script. Hope this demonstrates what I mean.
thanks
why_bird
update: I realise I've asked a similar question before (albeit in a more rambling fashion). I thought untie(@array) would have the same effect as close(FILEHANDLE), but it doesn't seem to. Making sure I'd close'd my file before did the trick, but untie-ing doesn't seem to have the same effect.
update 2: Actually, as Moritz pointed out, the file is being written too early, not too late.. d'oh
........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.