LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-08-2007, 07:16 AM   #1
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Rep: Reputation: 32
what is wrong with this perl code ? STDOUT / STDERR issue ?


Code:
#!/usr/bin/perl -w

print $ARGV[0],$ARGV[1];

my $ctr;
for ( $ctr = 0; $ctr < 50000000; $ctr++ ) { print ""; } # just do nothing

while ( 1 ) { print "test2\n"; sleep 1;}
this is just a heavily simplified version of my code, but exhibits the same strange behaviour.

I expect the code to do this:

- immediately print command line parameter 0 and 1
- run the for loop
- print "test2" forever


what happens on the console is this

- first it waits (for loop)
- then it prints $ARGV[0] ....
- then it runs the while loop

if I let it print to STDERR instead of STDOUT it does what I want, except that I don't want it to print to STDERR... I just don't understand what the heck is going on!

Last edited by rtspitz; 10-08-2007 at 07:18 AM.
 
Old 10-08-2007, 07:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Ran into this a while ago myself. I don't know the ins and outs, but this has to do with perl keeping stuff in a buffer before actually releasing/printing it.

I solved it this way:
Code:
#!/usr/bin/perl -w

$| =1; # don't keep in buffer

print $ARGV[0],$ARGV[1];

my $ctr;
for ( $ctr = 0; $ctr < 50000000; $ctr++ ) { print ""; } # just do nothing

while ( 1 ) { print "test2\n"; sleep 1;}
Hope this helps.
 
Old 10-08-2007, 12:26 PM   #3
rtspitz
Member
 
Registered: Jan 2005
Location: germany
Distribution: suse, opensuse, debian, others for testing
Posts: 307

Original Poster
Rep: Reputation: 32
sneaky bastard that perl, should've consulted the cookbook fist ;-)

Arrrr !
 
Old 10-08-2007, 12:32 PM   #4
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by rtspitz View Post
sneaky bastard that perl, should've consulted the cookbook fist ;-)

Arrrr !
Keep that in mind with other languages too. I know python does the same thing.
 
Old 10-08-2007, 06:53 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
By default, OSes and langs tend to buffer a certain amt of data before sending to stdout.
This buffering speeds up the program and lessens disk wear 'n tear.
As shown above, you can force no-buffering in Perl if you need to.
I tend to use that for logfiles, as it's usually the last msg befoe a prog breaks that tells you why, but this msg 'can' be lost if the prog fails to flush the buffer before die-ing.
(Actually, I rarely use the 'die' cmd. I always try to do a controlled exit that closes files/ db handles and logs/emails error, but that only works for anticipated errors, Sometimes things just die...)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
send stderr to a file and also to stdout pranavchoudhary Linux - Newbie 7 08-25-2008 01:57 AM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 03:15 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
Reopen the stdout and stderr rahul_kulkarni Programming 3 02-21-2005 06:55 AM


All times are GMT -5. The time now is 12:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration