Re: perl flawed or my fault



anno4000@xxxxxxxxxxxxxxxxxxxxxx wrote:
Eric Schwartz <emschwar@xxxxxxxxx> wrote in comp.lang.perl.misc:
"John W. Krahn" <someone@xxxxxxxxxxx> writes:

defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');
Or better yet, write it out:

if (defined($something)) {
$note_whome = $something;
} else {
$otherthing = 'singapore';
}

I always recommend against the ?: operator unless it's for something
small and self-contained. I avoid using it except for rvalues, unless
not using it will make things unacceptably ugly. Yes, this is a vague
standard. Here' an exampe of what is acceptable:

my $foo = $bar ? 0 : 1;

Here's something that isn't:

($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else,
'maybe') or die "um, I dunno") : call_cows($potatoes);

One point about ternary ?: is that it is an expression that has a
value, not primarily a means of flow control. The value should be
the main point if the construct is used, not the side-effects of
the code in the branches. In my view, small and self-contained
enters the image only in so far as it can be hard to find a readable
format for arbitrarily nested ?: with big complex branches. If
such a format can be found, there is no limit to the complexity.

I am of a split mind about using ?: in lvalue context. It *is*
kind of obscure, but there are situations that simply call for
it. One is the loop of a binary search, for example

while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.

I assume that you meant to write:

( $list->[ $mid] le $targ ? $low : $high ) = $mid;

because your example never assigns a value to $low.



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.