exit() argument evaluation

Discussion in 'Perl Misc' started by Dr. Peter Dintelmann, Sep 8, 2003.

  1. Hi,

    perl -le 'print 4==7 ? 0 : 1'

    produces "1" as output and

    perl -e 'die 4==7 ? 0 : 1'

    produces "1 at -e line 1.". But

    perl -e 'exit 4==7 ? 0 : 1'

    sets the exit code to 4 instead of 1 (using brackets around the
    argument of exit() changes this).

    Is there any reason for this behaviour?

    Peter Dintelmann
    Dr. Peter Dintelmann, Sep 8, 2003
    #1
    1. Advertising

  2. "Dr. Peter Dintelmann" wrote:
    >
    > perl -le 'print 4==7 ? 0 : 1'
    >
    > produces "1" as output and
    >
    > perl -e 'die 4==7 ? 0 : 1'
    >
    > produces "1 at -e line 1.". But
    >
    > perl -e 'exit 4==7 ? 0 : 1'
    >
    > sets the exit code to 4 instead of 1 (using brackets around the
    > argument of exit() changes this).
    >
    > Is there any reason for this behaviour?



    perldoc perlop
    [snip]
    nonassoc named unary operators
    nonassoc < > <= >= lt gt le ge
    nonassoc == != <=> eq ne cmp
    left &
    left | ^
    left &&
    left ||
    nonassoc .. ...
    right ?:
    right = += -= *= etc.
    left , =>
    nonassoc list operators (rightward)


    exit (a named unary operator) has higher precedence than == while print
    and die (list operators (rightward)) have lower precedence.


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 8, 2003
    #2
    1. Advertising

  3. Dr. Peter Dintelmann

    Trent Curry Guest

    "John W. Krahn" <> wrote in message
    news:...
    > "Dr. Peter Dintelmann" wrote:
    > >
    > > perl -le 'print 4==7 ? 0 : 1'
    > >
    > > produces "1" as output and
    > >
    > > perl -e 'die 4==7 ? 0 : 1'
    > >
    > > produces "1 at -e line 1.". But
    > >
    > > perl -e 'exit 4==7 ? 0 : 1'
    > >
    > > sets the exit code to 4 instead of 1 (using brackets around the
    > > argument of exit() changes this).
    > >
    > > Is there any reason for this behaviour?

    >
    >
    > perldoc perlop
    > [snip]
    > nonassoc named unary operators
    > nonassoc < > <= >= lt gt le ge
    > nonassoc == != <=> eq ne cmp
    > left &
    > left | ^
    > left &&
    > left ||
    > nonassoc .. ...
    > right ?:
    > right = += -= *= etc.
    > left , =>
    > nonassoc list operators (rightward)
    >
    >
    > exit (a named unary operator) has higher precedence than == while print
    > and die (list operators (rightward)) have lower precedence.


    Quite right. So the moral of this story is, when in doubt, add a set of ()'s
    the expression in question:

    perl -e 'exit (4==7 ? 0 : 1)'
    Trent Curry, Sep 8, 2003
    #3
  4. Hi John,

    "John W. Krahn" <> wrote in message
    news:...

    [snip]

    > exit (a named unary operator) has higher precedence than == while print
    > and die (list operators (rightward)) have lower precedence.



    ok; this was a fairly stupid question, sorry. It seems that I expected
    unary builtins to behave like list operators called with a one element
    list. Is this counter-intuitive ;-)?

    Peter
    Dr. Peter Dintelmann, Sep 10, 2003
    #4
  5. Dr. Peter Dintelmann

    Trent Curry Guest

    Dr. Peter Dintelmann wrote:
    > Hi John,
    >
    > "John W. Krahn" <> wrote in message
    > news:...
    >
    > [snip]
    >
    >> exit (a named unary operator) has higher precedence than == while
    >> print and die (list operators (rightward)) have lower precedence.

    >
    >
    > ok; this was a fairly stupid question, sorry. It seems that I
    > expected unary builtins to behave like list operators called with
    > a one element list. Is this counter-intuitive ;-)?
    >
    > Peter


    Wel, I'd say that seems to be how it (read unary) is supposed to behave. If
    you use it as you've written it in your subject lines (with the ()s ), then
    that solves it.
    Trent Curry, Sep 10, 2003
    #5
  6. "Dr. Peter Dintelmann" <> wrote:
    > ok; this was a fairly stupid question, sorry. It seems that I expected
    > unary builtins to behave like list operators called with a one element
    > list. Is this counter-intuitive ;-)?


    This behavior was most likely chosen to follow the DWIM philosophy --
    more often than not, you want named unary operators to grab just one
    thing. If they behaved like list operators, and grabbed more than one
    thing, what would they do with the extra?

    Also, when it doesn't DWIM, it is more likely to generate an obvious
    error, rather than eliminate parts or the whole of the following
    expression. [Odd behavior is more noticeable than missing behavior.]
    The result of a DDWIM [Doesn't DWIM] is more likely to be easily
    associated [by the programmer] with the first "list" element, and
    using parens should follow.

    It might be interesting to have a pipe syntax, so you could have
    written:

    4==7 ? 0 : 1 | exit

    [But then when 4 *does* equal 7, is the result 0, exit(0), or exit(1)?
    You'd still need parens.]
    Quantum Mechanic, Sep 10, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,873
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,787
    sandeep1976
    Nov 8, 2006
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    378
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    713
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    317
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page