Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I seem to have a problem that is impossible to fix. I have one program that runs the following system call. The program containing the system call detects when an input file is submitted and proceeds with the system call.
system "perl 20_October_2004_K.pl G:/PUB_HLTH/Perl_program/DSR_calcul +ation/20_October_2004/Input/$job G:/PUB_HLTH/Perl_program/DSR_calcula +tion/20_October_2004/Output/$job_out";
The output file is created and specified (through convention) by the file submitter, to the above output directory.
The computer running these Perl programs is unable (for unknown reasons) to create the output file. It can only write to the output file. But I want multiple writings to the output file because the input file can contain multiple queries in the single file. The perl program 20_October_2004_K.pl won’t let me do this. It only lets me write one fetchrow_array.

The bit of code that writes to the output file is:
my $inputfile = $ARGV[0]; my $outputfile = $ARGV[1]; open (OUTPUT_FILE, "+>>", $outputfile) or die "Unable to open $outputfile: $^E\n"; while (my @row = $sth_C->fetchrow_array) { print OUTPUT_FILE join("\t", @row); print OUTPUT_FILE "\n"; }
I don’t think I can solve this problem without big changes to the code. Any ideas?

Replies are listed 'Best First'.
Re: System call
by Roy Johnson (Monsignor) on Dec 21, 2004 at 18:34 UTC
    It seems to me that there is no reason to use system to call your perl script. Just set local values for @ARGV and call do:
    # instead of system perl... { local @ARGV = ("G:/PUB_HLTH/blah/blah", "G:/PUB_HLTH/blah/blah2"); do '20_October_2004_K.pl'; }
    I do not know whether this will fix your problem, but it might. You don't seem to close the file, and if you have a series of several different processes that open and append to the file without closing it, they may be stepping on each others' toes (although as each process exits, it should implicitly close the file).

    Caution: Contents may have been coded under pressure.
      I have fixed the problem now. But I am interested to know whether there would be advantages to using this method that I don't know about.
        You would have fewer processes running (so less resources are consumed), and any global variables from the master script would be visible in the auxiliary script.

        Caution: Contents may have been coded under pressure.
Re: System call
by castaway (Parson) on Dec 21, 2004 at 15:51 UTC
    What do you mean by 'unable'? Does it throw errors? (which?), does it just not append, and overwrite instead? etc..

    C.

      I have managed to fix the problem with a change to the table that I was querying. An SQL problem ... sorry about that folks.
      No error messages ... it just gives the result of the first query specified in the input file, and not the subsequent queries.
Re: System call
by dragonchild (Archbishop) on Dec 21, 2004 at 15:52 UTC
    Are you looping over the queries, calling prepare() and execute() within that loop?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Along the lines of:
      my $Return_results = "Select * from Result_storage_keep where Unique_ +identifier LIKE ".$Request_id." AND Aggregated_area = Instance_name A +ND Disease_cat = Cause_catagory\;"; my $sth_C = $dbh->prepare($Return_results) or die "Couldn't prepare +query: ".$dbh->errstr; $sth_C->execute() or die "Couldn't execute query: ".$sth_C->errstr;
      Yes