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
.......

In reply to Waiting for a file to be written by why_bird

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.