Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Does die() change the order of execution?

by lo_tech (Scribe)
on Nov 01, 2001 at 21:47 UTC ( #122607=perlquestion: print w/replies, xml ) Need Help??

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?

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://122607]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-02-26 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?