Re: perl flawed or my fault

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.


Relevant Pages

  • Re: perl flawed or my fault
    ... I avoid using it except for rvalues, ... with big complex branches. ... such a format can be found, there is no limit to the complexity. ... certain sorts of tools at low cost and in short order. ...
  • Re: Proper format when rsyncing to thumb drive
    ... folks here recommend to minimize 'wear' on the usb drive or other ... Format it as ext3. ... Note that I have found that rsync sometimes crashes on writing to usb ... You disagree with the others in your recommendation to use ext3. ...
  • RE: date format setting
    ... I recommend that you create a local Access table that you are going to ... Format() function to define the proper format for inserting into Oracle. ...
  • Re: Please recommend a 73GB Disk drive for an Ultra 2 Enterprise
    ... I can recommend both 15k and 147GB, as returned by format: ... performance is worth every dime if you work with thousands of small ... unless you are running some insane database query. ...
  • Re: Long Doc Formatting Assistance
    ... I am helping the editor of a journal to format books. ... the header and both must show. ... and two inch margins on the sides. ... If yes, how would you recommend ...