in reply to Help with a logical structure for an if statement

Missing '~' in your code.
It's often better to use 'and' and 'or' (lower precedence than '&&' and '||')
update: and it's better to use 'eq' than regex when you don't really want a regex. :-)
  • Comment on Re: Help with a logical structure for an if statement

Replies are listed 'Best First'.
Re^2: Help with a logical structure for an if statement
by JavaFan (Canon) on Apr 29, 2012 at 19:32 UTC
    It's often better to use 'and' and 'or' (lower precedence than '&&' and '||')
    I don't get this. Lower precedence operators are better to use than higher precedence ones, even if they have existed in Perl longer, and are used in a myriad of other languages? If so, do you have some arguments to back up this claim?

    Perhaps you meant to write "I would use" where you wrote "It's often better to use"?

    I would write it as:

    if ($player_one_race eq 'terran' && $player_two_race eq 'zerg' || $player_one_race eq 'zerg' && $player_two_race eq 'terran') { ... }
    That is, I'd lose the redundant parenthesis, use the standard operators, and use whitespace to make the symmetry clear. Or:
    my %races; $races{$_} = 1 for $player_one_race, $player_two_race; if ($races{zerg} && $races{terran}) { ... }

      Or even:

      if ([sort $player_one_race, $player_two_race] ~~ [qw(terran zerg)]) { # TIMTOWTDI }
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Oops. I'm not sure to understand what you said but here some explanation with my well-known "beach-English".
      Because and and or have the lower precedence ( lower than = for example), it is for me simpler : no need to use extra parenthesis when they are used as 'if...then' way.
      With && and || I always have to check 'perldoc perlop' to find precedence. So, OK, it's only my choice.
      Some not tested examples I would write
      And: $var=$den and $m/=$var
      &&: $var=$one&&$two