[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Checking user-id's and paswords



At 20:48 +1000 on 14/7/97, Jon Warbrick wrote:
>  if ($xxx eq 'true')
>  { print "OK\n";
>  }
>  else
>  { print "Nogo\n";
>  }

You probably know this already.. but this "if" statement could be written
much more concisely as:

 print ($xxx eq 'true') ? "OK" : "Nogo", "\n";

That's the ternary conditional operator. To quote from the perlop.pod pages
(Operators and Precedence, or command-click on a ?)

  Conditional Operator

  Ternary "?:" is the conditional operator, just as in C.  It
  works much like an if-then-else.  If the argument before the
  ? is true, the argument before the : is returned, otherwise
  the argument after the : is returned.  For example:

      printf "I have %d dog%s.\n", $n,
              ($n == 1) ? '' : "s";

This operator is very useful (read: was designed for) those situations
where the "if" only selects between two different values to assign to one
variable, or two different variables to assign a particular value...

  $a = (expr) ? "true value" : "false value";
  ((expr) ? $true_variable : $false_variable) = "value";

Anyway... enough of teaching grandma to suck eggs...

-Alex Satrapa

Windows 95: n. 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company.



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch