stdout order of perl script (UNIX)

my perl script has parts that look like this:

print "==== doing something 1 ====
";
system(myprog bla);
print "==== doing something 2 ====
";
system(myprog bla2);

If I run the script in the shell, I get the correct output order -something like:

==== doing something 1 ====
myprog is running for bla
==== doing something 2 ====
myprog is running for bla2

BUT if I redirect the output (using myscript.pl > tmp.out) then the output is not in the correct order:

myprog is running for bla
myprog is running for bla2
==== doing something 1 ====
==== doing something 2 ====

What is the cause for this and how do I fix it?

thnx
Keren





Comments

  • : my perl script has parts that look like this:
    :
    : print "==== doing something 1 ====
    ";
    : system(myprog bla);
    : print "==== doing something 2 ====
    ";
    : system(myprog bla2);
    :
    : If I run the script in the shell, I get the correct output order -something like:
    :
    : ==== doing something 1 ====
    : myprog is running for bla
    : ==== doing something 2 ====
    : myprog is running for bla2
    :
    : BUT if I redirect the output (using myscript.pl > tmp.out) then the output is not in the correct order:
    :
    : myprog is running for bla
    : myprog is running for bla2
    : ==== doing something 1 ====
    : ==== doing something 2 ====
    :
    : What is the cause for this and how do I fix it?
    :
    : thnx
    : Keren
    :
    :
    Odd....I've never tried outputting to a file that way. Why is this neccessary?
    -----------------------
    "The three principle virtues of a programmer are laziness, impatience, and hubris"

  • Hi,

    I wonder, could it be something to do with buffers not flushing properly or something? I think there is something you can do in Perl to change when a the buffer flushes or there may be a flush function/sub/command that you could call. I can't remember either, but if you or someone can dredge those up, it may be worth a try.

    Jonathan



    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

  • In reply to your question - why it is necessary to use the redirection operator ">" - the answer is, it is simply useful. Think about it. If you have programs that output to standard output, lets say you didnt write them, like "ls" or "grep" or any other prog, and you want all their collective output (lets say they are called many times by a perl script, for example, do "ls" for each directory in the cwd). Another example, you want to run "grep", and then run a perl script on the result - better store the grep result in a tmp file and run the perl script on that file.

    In short, I can't imagine working without it.

    K

    : : my perl script has parts that look like this:
    : :
    : : print "==== doing something 1 ====
    ";
    : : system(myprog bla);
    : : print "==== doing something 2 ====
    ";
    : : system(myprog bla2);
    : :
    : : If I run the script in the shell, I get the correct output order -something like:
    : :
    : : ==== doing something 1 ====
    : : myprog is running for bla
    : : ==== doing something 2 ====
    : : myprog is running for bla2
    : :
    : : BUT if I redirect the output (using myscript.pl > tmp.out) then the output is not in the correct order:
    : :
    : : myprog is running for bla
    : : myprog is running for bla2
    : : ==== doing something 1 ====
    : : ==== doing something 2 ====
    : :
    : : What is the cause for this and how do I fix it?
    : :
    : : thnx
    : : Keren
    : :
    : :
    : Odd....I've never tried outputting to a file that way. Why is this neccessary?
    : -----------------------
    : "The three principle virtues of a programmer are laziness, impatience, and hubris"
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories