Hi there I have a program that has been written to perform a SQL query (not a particularly slow or complicated one), write the results to a file and then extract information to be used in another program. This is the code so far.
#!/usr/local/bin/perl -w # General perl libraries use strict; use English; use DBI; # output file my $outputfile = $ARGV[0]; open(FILE, ">>$outputfile") || die "Error: Can't open $outputfile for writing: $!\n"; my $dbh = DBI->connect("dbi:Pg:database;host=dbhost", "user", "userpwd", {AutoCommit => 1}); my $sth = $dbh->prepare("Tried and tested SQL query that simply return +s a list of id numbers"); $sth->execute(); #print values while(my $ref = $sth->fetchrow_hashref()) { print FILE "$ref->{'id'}\n"; } $sth->finish(); # disconnect from database $dbh->disconnect(); SendtoPrepare($outputfile); ################################################################### sub SendtoPrepare { my($file) = @_; print "$file\n"; open(INFILE, "$file") || die "Error: Can't open $file for reading: $!\n"; my @code = <INFILE>; print "@code\n"; }
Quite simple really but what i dont understand is that the code wont print out the @code array unless I run the script twice. The file the subroutine reads is created on the first run but I think its not created quickly enough to then be processed in the subroutine. Hence why the subroutine only prints out the array after the second run (because the file was obviously been completely written by then).
I guess this problem is due to wanting to do something with the file before its completely written but who might I halt the program slightly so that I ensure the file is written before going though to the subroutine?
Any help much appreciated

In reply to slowing down perl script to ensure file is written before program tries to process it in subroutine by Angharad

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.