>>>>> "SB" == Steve Bertrand <st...@ibctech.ca> writes:
SB> Besides consistently forgetting how to properly spell "ternary", I SB> can't, for some reason, embed it's use into my brain no matter how SB> much I read. ternary op is an official name but it has many nicknames so they are worth knowing too. conditional expression is longer but very descriptive. hook/colon is a slang name. it is actually very simple to understand. the key point to knowing it and how to use it is that the 2 value expressions SHOULD NOT have side effects. that means changing something by assignment or other modification. your example below is one exception to that. SB> Perhaps if someone could show me the way against a personal code snip, I SB> may finally "get it". What would this look like: SB> my $gst = $self->query->param( "gst${item_num}" ); SB> my $pst = $self->query->param( "pst${item_num}" ); SB> if ( $gst eq 'Yes' ) { SB> $gst = $self->tax_rate( 'gst' ); SB> } SB> else { SB> $gst = 0; SB> } whenever you see an if/else where the code just assigns one or another value to the same variable that should be coded as a ternary op as this is what is it designed to do: $gst = ( $gst eq 'Yes' ) ? $self->tax_rate( 'gst' ) : 0 ; now, using the same variable $gst for the input and then a value is slightly dodgy IMO. and i usually put () around the conditional part to highlight it though it isn't necessary. it is one place i like the extra parens. SB> ...in some cases I get it ( when reading code ). Then, shortly after SB> when I try it, my code breaks. SB> I'm thinking that I'm running into a precedence issue, but normally I SB> don't have so much trouble remembering a seemingly simple idiom. if you don't do side effect (assignments) then precedence shouldn't be a problem. the issue is that = binds tighter than ?: so you don't get what you think you are doing. this is common dumbass code as it breaks what the conditional op is for and also breaks the code because of precedence: ( $foo == 0 ) ? $bar = 1 : $bar = 2 ; i won't go deeper into the precedence bug but instead focus on the dumbass part. why is that coded assigning to the same var inside the ?: ? the whole point of ?: is to get a value from one of two choices to USE. so that should be written as: $bar = ( $foo == 0 ) ? 1 : 2 ; note how it is shorter, clearer, not redundant (only one mention of $bar) and actually correct. the goal of ?: is its value result, not the effects of executing one or the other expression. if you aren't using the result of ?: you are using it incorrectly (even if the code works). SB> my $args = ( ref $_[0] eq 'HASH' ) ? shift : {} ; SB> - if $_[0] is a hashref, shift @_ , and assign it to $args SB> - otherwise, assign an empty hashref ( anonymous ) to $args correct but you shouldn't read it that way. you mention assign twice and that is the same kind of redundancy that i showed above. it is better read as: check if $_[0] is a hash ref and if so assign it (from the implied shift of @_) else an anon hash to $args. but ?: is a very common op and should be in your arsenal and you shouldn't need to translate it to english. especially if you obey the rule about not doing assignments or most side effects in the expressions. 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/