>>>>> "r" == rkb <r...@i.frys.com> writes: r> Uri Guttman wrote: >> when will people learn?? the ternary operator is for >> CHOOSING A >> VALUE. it is NOT for side effects. its precedence is >> designed to allow >> it to be assigned, NOT to have assignments done inside. >> >> >> NEVER USE ?: for side effects. ALWAYS use it to return a >> value chosen >> from two. do not break this rule and it will always work. >> >> $bar = ($foo == 1) ? 0 : 1 ; >> >> uri
r> I looked through perlop and was unable to find where it r> stated that the ternary operator should only be used in an r> assignment such as you've shown. Can you point out where r> that's located. it doesn't state that but given its origin, the issues with precedence, its mail purpose, it is very obvious to me that it shouldn't be used for side effects. it is designed to return an expression so why use it when you don't return anything? r> I often use the ternary operator when working with r> dispatch tables. Based on your comment, I suspect that r> you feel that the following example is a poor use of the r> ternary operator. Please correct me if I'm wrong. r> exists $dispatch{$status} ? $dispatch{$status}->() r> : $dispatch{'error'}->(); you don't need exists on a dispatch table. and i code them like this: my $code = $dispatch{$status} || $dispatch{'error'} ; $code->() ; very clean, no duplication of $dispatch{$status} or the code call, easy to see the error case. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/