I was reading Mark-Jason Dominus' article "How Regexes Work" in TPJ Issue #9, Vol 3, No.1 and noticed one thing he did:
print 'It ', ($result ? 'did ' : 'did not'), "match\n";
Assuming that $result is 0 or 1, I would concisify this thusly:
print 'It ', ('did' 'did not')[$result], "match\n";

Replies are listed 'Best First'.
RE: An alternative to ?: notation
by agoth (Chaplain) on Aug 09, 2000 at 18:56 UTC
    apart from a missing comma my first reaction to your 'concise' version is ouch, followed by wince, then yeuck.
    Personally i think the ternary method gives a lot more in readability than it loses in verbosity and is probably easier to maintain in the long run. Just my $0.02.
      You are right, of course. The ternary method is both a standard recognizable construct and should be faster. (No temporary array to build and destroy.) I certainly would not recommend switching.

      OTOH I appreciated princepawn's suggestion for the amusement value. Sometimes you just need to put a joke in your code for sanity's sake, and I might use this someday for just that purpose... :-)

(Ovid) RE: An alternative to ?: notation
by Ovid (Cardinal) on Aug 10, 2000 at 01:10 UTC
    Assuming that $result is 0 or 1

    That's a mighty big assumption to make. In this case the ternary operator is far more maintainable. Someone's going to have a *fun* debugging session if the author of the ternary operator has allowed for result being undef,"Bob" or 17.

    The ternary operator allows for a consise testing of true/false and returns a value. The power of this of this operator, if properly exploited and understood, is its ability to generate a concise bit of code. What if we have user input and we need to return a value (computed in a subroutine) based upon their input, or a default value if they gave no input (or an input that evaluates as false)?

    $result = $input ? somesub($input) : $default;
    Consider the alternative:
    if ($input) { $result = somesub($input); } else { $result = $default; }
    I know which I'm putting in my code.

    Cheers,
    Ovid

RE: An alternative to ?: notation
by ZZamboni (Curate) on Aug 10, 2000 at 18:05 UTC
    Apart from what others have said, these two construct (even if you can assume that $result will be 0 or 1) are not semantically equivalent if the expressions have side effects (for example, if one of them is a call to a subroutine that reads a file, computes a result, and removes the file before returning). In the array case, both expressions are always evaluated, whereas in the ternary operator only the one that corresponds to the decision value is. As a silly example, consider the following two expressions:
    $result==0?"zero":5/$result; ("zero", 5/$result)[$result];
    The second one will produce a runtime error when $result==0.

    --ZZamboni

RE: An alternative to ?: notation
by Cirollo (Friar) on Aug 09, 2000 at 18:59 UTC
    Obviously TIMTOWTDT, but I think that method would be overly confusing - being concise is a good thing, but IMHO it is far better to use a few more characters and clearly state your intentions. The first method is clearly a conditional; the second is confusing at best.

    Oh, and you forgot the comma in your ('did', 'did not') list :) What do you think this is, lisp? :P

    Update:Argh, agoth beat me on this one... need to be quicker on the draw and not get distracted while I'm trying to post {grin}
    buzzcutbuddha: my sentiments exactly :)

      Yeah, I hate it when work gets in the way of posting on PM. What do my bosses and co-workers think I'm here for? I came for the permissive dress code and T-1 so I could surf all day. Didn't everybody? :)