in reply to Re: Complex conditional sort
in thread Complex conditional sort

I'd recommend against using the negation operator to return a positive value for true, not so much because there no guarantee that it will continue to do so, but because the reader probably won't know it returns positive for true. (Especially since some languages use a negative value for true!) It's not like $x && 1 or $x ?1:0 would be a burden to use instead of !!$x.

Replies are listed 'Best First'.
Re^3: Complex conditional sort
by wind (Priest) on Mar 09, 2011 at 03:50 UTC

    Fair enough ikegami.

    I like using the !!$test construct to force a boolean to 1 or (''/0). However, if that isn't clear enough, I'd probably choose using the >0 test.

    sort {$products{Cat}{Pro}{$b}{Qua}>0 <=> $products{Cat}{Pro}{$a}{Q +ua}>0 || $a <=> $b}

    I suppose it still requires some level of understanding of the possible return values of conditionals, but that's one of the most important things to be familiar with when dealing with sorting in my opinion. Especially the <=> and cmp operators.

      but that's one of the most important things to be familiar with when dealing with sorting in my opinion. Especially the <=> and cmp operators.

      Only those. Again, Perl makes no guarantee as to what the others return. It's definitely not important to be familiar with what they return as they can return anything true and anything false. In fact, they don't always return the values you think they return.

      I'd probably choose using the >0 test.

      Same exact problem as negation.

        I figured you'd say that. I hear ya when it comes to perl not "promising" any particular return value. But can you name any example where !!$test doesn't return 1 or ''/0 currently? And do you really think it's likely that's going to change?

        Obviously, for safety sake with future versions, it probably is better to rely on documented return values instead of assumed current implementation. However, it seems a little silly to me to be that wary of something so basic changing. I wouldn't have been the only person to occasionally take advantage of observed behavior instead of only relying on documented. To future ill or not.