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

In my quest to reduce vertical space and advance my perl horizon :^) I am trying to convert my subroutine return statements to use the ternary operator.

Original:
sub didScriptSucceed { if (0 eq system "$KSH_SCRIPT") { return 1; } else { return 0; } }
Failed Attempt:
sub didScriptSucceed { return (0 eq system "$KSH_SCRIPT"?$1:2); }
Thanks in advance for anyone who can provide some insight.
Cheers, ETP

Replies are listed 'Best First'.
Re: subroutine return ternary output
by muntfish (Chaplain) on Sep 07, 2004 at 14:12 UTC

    It looks like a typo to me: you have $1 in the second example, and this is probably not defined. Also your brackets may be misplaced.

    This should work in the same way as your original:

    return (0 eq system "$KSH_SCRIPT") ? 1 : 0;

    Now, whether you should be comparing the return code of system with "0" (eq is a string comparison) is a different matter...


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
      Yes muntfish I did create a typo when submitting to perlmonks.
      return (0 eq system "$KSH_SCRIPT"?$1:2);
      should be...
      return (0 eq system "$KSH_SCRIPT"?1:0);
      Using your advice and changing my paran's made it work:
      return (0 eq system "$KSH_SCRIPT")?1:0;
      All is well now. Interestingly enough both "eq" and "==" work as expected on testing the return code. I assume it comes back as an integer and perl converts for us? Thanks for the help!!
Re: subroutine return ternary output
by Random_Walk (Prior) on Sep 07, 2004 at 14:41 UTC
    Not playing with the ternary x?y:z but here is another approach
    return not (system "$KSH_SCRIPT")

    Cheers,
    R.

Re: subroutine return ternary output
by shenme (Priest) on Sep 07, 2004 at 14:26 UTC
    When in doubt (and many other times also) use parentheses: they're there to keep your brains from dribbling sideways. ;) What does this do for you:
      return ( (0 == system($KSH_SCRIPT)) ? 1 : 2 );
Re: subroutine return ternary output
by eserte (Deacon) on Sep 07, 2004 at 14:12 UTC
    Is $1 a typo? And why are you returning 2 while the original returned 1 on failure?
Re: subroutine return ternary output
by ysth (Canon) on Sep 07, 2004 at 17:39 UTC
    Your "0 eq system" is troublesome; most places where perl returns a false boolean result use a special value that's "" in string context and 0 in numeric context. If you try to do a string compare against zero, it will always fail:
    # both of these print nothing perl -we'$x = 1; print "hmm" if 0 eq not $x' perl -we'$x = 0; print "hmm" if 0 eq not $x'
    System is an exception to this since it actually returns the number 0, which is "0" in string context, but it's still a bad habit to get into. At least make it 0 ==, not 0 eq.
      Thanks ysth,
      I have updated the condition to do a numerical comparison. My focus was on the ternary operator, so I brain farted and used the string operator.
Re: subroutine return ternary output
by Anonymous Monk on Sep 07, 2004 at 15:08 UTC
    sub didScriptSucceed {!system $KSH_SCRIPT}
Re: subroutine return ternary output
by sintadil (Pilgrim) on Sep 08, 2004 at 01:27 UTC

    In my quest to reduce vertical space and advance my perl horizon :^) I am trying to convert my subroutine return statements to use the ternary operator.

    If you're having problems "optimising" your code with the ternary operator, perhaps you should avoid doing so until you truly understand how to use said operator, lest your "optimisations" decrease your code readability and maintainability (and, as seen here, its "workingness" :)).

      Thanks, that's the reason I posted the question. To "advance my perl horizon". If I had all the answers, then I wouldn't need to post questions to the monks :)