Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print to OUTFILE, not STDOUT

Status
Not open for further replies.

jvchamary

Programmer
Feb 4, 2004
9
0
0
CH
Hello experts!

I've been writing scripts to answer biological questions in evolutionary genomics for just over a year and have recently come across a rather strange problem involving STDOUT.

To write my data to an output file, I have written a subroutine so that I can print to several outfiles without having to create a filehandle for each. It also saves on space ;)

sub output2file {
my($OUT, $printout) = @_;
open (OUT, ">>$OUT");
print OUT "$printout";
close (OUT);
}

I appended this subroutine to my perl template several months ago (so it's present in all my scripts), which enables me to print out like so:

output2file("outfile.txt", "$results");

I can run a second script ('script2.pl') from 'script1.pl' using backticks (although using system and exec makes no difference):
`perl script2.pl some_arguements`;

However, '$results' (the result of running 'script2.pl'), is printed to STDOUT instead of being printed to the outfile.

# running other script2s using backticks creates output files
# running 'script2.pl' from the command line creates an outfile, but this is no longer the case after 'script1.pl' has been invoked (therefore it can't be a bug/typo)!

Would Perl print to STDOUT as a default if an exception was returned in 'script2.pl'? I use warnings, so surely it would warn me if this were so! Besides, my $results from STDOUT look correct! What is stopping 'script2.pl' from printing $results to the outfile?

Thanks in advance for any help that you can give,

J. Chamary

(version 5.8.0, using -w, XPpro).
 
Some code about how you are trying to capture the output of script2.pl would be nice.
 
Ah, thanks for that, obviously I wasn't clear enough. :D

I'm not trying to capture the output! All 'script1.pl' does is create and then analyse text files (using 'script2.pl') in turn, i.e.

@Is = (0..2);
foreach $i (@Is) {
# printout $results_$i from 'script1.pl' to output file
# analyse output file using 'script2.pl'
}

'script2.pl' should produce its own outfile, instead the results are displayed.
 
And you sub output2file takes care of outputting script2 for you? If every other file outputs correctly, then there I guess there is something that is not being passed properly in reference to script2. Make sure that sub is actually being invoked for script2. If the sub is running, make sure that the sub is receiving the correct values for both $OUT and $PRINTOUT. Are you using warnings in your script? If not, turn them on.

#!/usr/bin/perl -w

 
strange ... could you try select OUT to see if that makes any difference to your subroutine?


Kind Regards
Duncan
 
Dumbing it down to the base case works perfectly fine here.
Code:
#script1.pl
sub output2file {
    my($OUT, $printout) = @_;
    open (OUT, ">>$OUT");
    print OUT "$printout";
    close (OUT);
}

output2file('outfile.txt','this is in script one');
system('perl','script2.pl','param');

#script2.pl
sub output2file {
    my($OUT, $printout) = @_;
    open (OUT, ">>$OUT");
    print OUT "$printout";
    close (OUT);
}

output2file('outfile.txt',$ARGV[0]);
If that works for you, then I think we'll need some more details on what else the script(s) is doing, in particular, any other i/o.

________________________________________
Andrew - Perl Monkey
 
Thanks for the suggestions, unfortunately I've been extremely busy over the last few days and have only just had a chance to try them out.

raklet
# both 'script1.pl' and 'script2.pl' contain the output2file subroutine
# the sub is being evoked, as I have test-printed to STDOUT from it
# both $OUT and $printout are recognised, i.e. become "outfile.txt" and "$results" respectively
# -w, I never start without it :D

duncdude
# using 'select OUT' makes no difference

icrf
# the base worked fine for me too. I gradually re-hashed each line/loop of 'script2.pl' and find that it works fine on it's own. However, when I run 'script1.pl' to invoke 'script2.pl' my results are still printed to STDOUT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top