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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP - Perl print command - believe it or not!

Status
Not open for further replies.

Captainrave

Technical User
Nov 16, 2007
97
GB
I need help.

I have a number of scripts which analyse a file and print the output either onscreen or to an outfile. On a colleagues computer they work as they are designed to. However on my computer none of them work correctly. I get errors such as:

print() on closed filehandle OUT at DirectRepeatfinder.pl line 344

Or sometimes nothing even happens. It definitly isnt the code because its just a simple print command!...

print OUT "FT repeat_unit ", "$START", "..", "$STOP", "\n", "FT /label= 1", "\n";

Is this a recognised problem? Maybe with filepermissions or something?
 
As a further test I wrote this script...

#!C:/Perl/bin/perl.exe -w
$display = 'Hi there/n';
print "$display";
sleep 5;
exit;

The command window simply comes up and does nothing!
 
Do you have Active Perl installed in your windows machine.
I assumed its windows by looking at the code.
Whats the extension of the file name you are executing within the command line?
Is it ".pl" without the quotes?
FYI, I had no issues executing the script u mentioned.
 
Fixed your test script


#!C:/Perl/bin/perl.exe -w
$display = "Hi there\n";
print $display;
sleep 5;
exit;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
So that script seems to work. But similar scripts dont? The following script that uses the same format, WILL NOT work! So annoying! It doesnt close with an error. It comes up with a blank screen, sleeps for 5, then exits! Why will it not print $DNA?

#!C:/Perl/bin/perl.exe -w
$DNA = "ACGGGAGGACCCCGGGGGAAAAATTATATAGCGA";
print $DNA;
sleep 5;
exit;
 
You script works in my environment - cygwin on XP - but relies on the program exit to flush the output buffer. If you open a window and run the script, the output does not appear until after the sleep. If you are usiing a dedicated window which cloes on program termination, you may not see it at all.

travs69's script prints a newline at the end of the string, which is why it appears to behave differently.

You can turn off buffering by setting the magic variable $| (that's a pipe symbol) to 1. This works on the currently selected output handle, which is STDOUT in your simple code. If you use an explicit handle, you need to [tt]select()[/tt] it and then set $|.

None of this affects your original problem, where the error message clearly states that filehandle [tt]OUT[/tt] is closed. This would arise if [tt]OUT[/tt] had never been opened, had been explicitly closed, had encountered and error or had been declared with [tt]my[/tt] and had subsequently gone out of scope.Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
Code:
#!C:/Perl/bin/perl.exe -w
$DNA = "ACGGGAGGACCCCGGGGGAAAAATTATATAGCGA[COLOR=red]\n[/color]";
print $DNA;
sleep 5;
exit;

Could be to do with buffering, if the above script works, then it's because your not outputting a line seperator to flush the buffer

Try adding this to the top of your script, BTW you never answered the question about activeperl, does this mean you have some other flavour installed
Code:
$|=1; #setting to nonzero forces a flush right away
see look for $OUTPUT_AUTOFLUSH
HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Missed that question. Im running the latest version of activeperl. Will try your suggestions now...
 
Ok, that solves one problem. When I open the perl file in a dedicated window it prints AFTER the sleep command. Thats very useful to know. At least there isnt a global perl problem on my computer. However I still have the problem with the original script that I've seen work on another computer, but doesnt work on my computer.

I've uploaded the Perl script and a sample file that it would be run on.

(Perl script)

(Sample txt file)
 
I'm getting the same issue as you, I'm running Perl on Vista here, same problem in XP.

I've a feeling that the machine it works on without issue has some non standard setup. But it could be that we 3 are not the norm, wouldn't surprise me :p

I'd try implementing a block that opens files, and then have the code write to the files, and then a block to close the files. It should increase the speed of your script as well, because it won't have the overhead of opening the file, seeking to the end and then closing the file. FWIW, I changed the OUT to OUT1, for the first file and the outcome was the same, so, it's not related to filehandle reuse, though I tend to avoid this as it can lead to confusion. Filehandles are cheap, trust me ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Try running the script with your antivirus disabled, it is possible it has control of your file while perl is trying to use it. I would also add some kind of print message during your iterations so you know it is doing something. If that fails I would start slimming down the script to it's most basic parts and see what you remove that make it stop failing.

And without setting $|=1 perl won't print anything till you hit a new line (\n). That's why my script worked and yours didn't.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top