Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

behaviour of STDOUT and STDERR

by thiagu_mvt (Sexton)
on May 14, 2009 at 11:01 UTC ( [id://764055]=perlquestion: print w/replies, xml ) Need Help??

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

print STDOUT "Output with Newline\n\n between"; print STDERR "FirstLine\nSecondLine\nThirdLine";
when executed it prints
[host1]$ perl out_err.pl Output with Newline FirstLine SecondLine ThirdLine between
Why is contents of stdout printed after stderr ? because each stdout and stderr are launched in different threads and stderr finished soon ?
can somebody explain accurately ?
-Thiagu

Replies are listed 'Best First'.
Re: behaviour of STDOUT and STDERR
by Burak (Chaplain) on May 14, 2009 at 11:06 UTC
Re: behaviour of STDOUT and STDERR
by linuxer (Curate) on May 14, 2009 at 11:06 UTC
Re: behaviour of STDOUT and STDERR
by marto (Cardinal) on May 14, 2009 at 11:10 UTC

    See suffering from buffering:

    #!/usr/bin/perl use strict; use warnings; $| = 1; print STDOUT "Output with Newline\n\n between"; print STDERR "FirstLine\nSecondLine\nThirdLine";

    Does what you expect

    Hope this helps

    Martin

Re: behaviour of STDOUT and STDERR
by thiagu_mvt (Sexton) on May 14, 2009 at 11:20 UTC
    yes it worked. Thanks all of you.
Re: behaviour of STDOUT and STDERR
by tbone1 (Monsignor) on May 15, 2009 at 12:21 UTC
    I don't have a lot to add, except maybe perspective so you understand why things are the way they are.

    Think of why STDOUT and STDERR exist seperately and what their first use is. STDOUT prints to the screen, log, report, etc. As long as the data gets there, you're okay. So STDOUT's default behavior is to write the contents of the buffer when it gets to it. (With modern computers, this isn't such a big deal, but "back in the day", it made for more efficient resource use.)

    STDERR, on the other hand, was designed to report error information. And if an error is going to make the process crash, you want that error information to be written right now so it gets stored before the process ends. So when something gets to STDERR it gets printed immediately, for good reason.

    Therefore, it is not surprising that things will get intertwined if you are writing STDOUT and STDERR to the same location, just by the nature of the default behaviors.

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764055]
Approved by Burak
Front-paged by linuxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-03-19 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found