in reply to ternary operator ?

It's essentially like a returning inline if statement. The first expression is the test, and if it's true it will execute the expression after the question mark, if it's false, it will execute the expression after the colon e.g
my $foo = "test"; print +( $foo =~ /^\w+\z/ ? "condition true" : "condition false" ),$/; my $bar = defined $x ? $x : "nought"; print "\$bar is: $bar", $/; __output__ condition true $bar is: nought
See the Conditional Operator section in the perlop manpage for more details.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: ternary operator ?
by Abigail-II (Bishop) on Nov 29, 2002 at 10:37 UTC
    I wouldn't say "execute", but rather "return". Why? Because it can also return lvalues. In ALGOL, one could do:
    IF a THEN b ELSE c FI := IF d THEN e ELSE f FI;

    which was great. You can do something like that in Perl using the ternary operator:

    ($a ? $b : $c) = ($d ? $e : $f);

    You can't do that in C or Java without use much more code.

    Abigail

      I wouldn't say "execute", but rather "return".
      So would I ;)
      It's essentially like a returning inline if statement.

      _________
      broquaint