http://qs1969.pair.com?node_id=122607

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

Could anyone explain this to me?

Here's the code:

#!/usr/bin/perl -Tw use strict; print $#ARGV; die "where are those args again?\n" unless ($#ARGV > 0); exit;

Here's the output without an @ARGV:

where are those args again?<br> -1

Here's the question:
Why is the print after the die? Server Error (Error ID 921132c2133309)

An error has occurred. The site administrators have been notified of the problem and will likely soon fix it. We thank you, for you're patients.

Replies are listed 'Best First'.
Re: Does die() change the order of execution?
by runrig (Abbot) on Nov 01, 2001 at 21:50 UTC
    STDOUT is buffered and may appear after STDERR. Put this at the top:
    $|++;
Re: Does die() change the order of execution?
by jwest (Friar) on Nov 01, 2001 at 21:53 UTC
    What you're seeing is an illusion. Since you're looking at STDOUT (the target of print) and STDERR (the target of die) at the same time, it looks like that's the order it's executing in. In all actuality, it's happening in the order you expect, but the buffers are being flushed in the "wrong" order, creating the discrepancy.

    Hope this helps!

    --jwest

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re: Does die() change the order of execution?
by mkmcconn (Chaplain) on Nov 01, 2001 at 22:00 UTC
Re: Does die() change the order of execution?
by Asim (Hermit) on Nov 01, 2001 at 21:58 UTC

    Sounds like a buffering issue. Perl buffers the output of commands (i.e. print statements using STDOUT) automatically, but (as I recall) error messages (using die which prints to STDERR) come out as soon as possible. Try using:
    $| = 1;
    after the use strict; and see what comes out.

    ----Asim, known to some as Woodrow.