exit() argument evaluation

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default exit() argument evaluation

    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 Guest

  2. Similar Questions and Discussions

    1. Evaluation Period
      I thought the evaluation period is 6 weeks. But now second week into my evaluation period, it tells me to switch to developer edition. Why? I'm...
    2. Evaluation upgrade
      We have been using the evaluation copy of windows 2003 for some time now. The evaluation period ran out a few days ago. We have purchased a retail...
    3. Site Evaluation Please
      Completed site & need your evaluation. Thanks. Referring URLs http://www.bertmoniz.com
    4. Flash evaluation
      Hi everyone. I am new to this newsgroup. I enjoy using flash and I have just finished a website created for two of my friends who are travelling...
    5. Unixware Evaluation
      On Sat, Jul 19, 2003 at 08:45:06PM +0100, Graham Wharton wrote: Perhaps you might get some ideas here: http://www.opensource.org/sco-vs-ibm.html...
  3. #2

    Default Re: exit() argument evaluation

    "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 Guest

  4. #3

    Default Re: exit() argument evaluation

    "John W. Krahn" <krahnj@acm.org> wrote in message
    news:3F5C4F58.A0C4CD38@acm.org...
    > "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 Guest

  5. #4

    Default Re: exit() argument evaluation

    Hi John,

    "John W. Krahn" <krahnj@acm.org> wrote in message
    news:3F5C4F58.A0C4CD38@acm.org...

    [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 Guest

  6. #5

    Default Re: exit() argument evaluation

    Dr. Peter Dintelmann wrote:
    > Hi John,
    >
    > "John W. Krahn" <krahnj@acm.org> wrote in message
    > news:3F5C4F58.A0C4CD38@acm.org...
    >
    > [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 Guest

  7. #6

    Default Re: exit() argument evaluation

    "Dr. Peter Dintelmann" <peter.dintelmann@dresdner-bank.com> 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139