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

Re: [MacPerl] Does Style Count? (formally, 'file tests')




The "predicate ? expression1: expression2" construct in Perl is not a
mere syntax sugar. It differs from "if then else" because the former
is an expression and the latter is not. More specifically, the former
returns a value. In practice, this means one can do:

my $var = (predicate ? expr1: expr2);

but not

my $var = if (predicate) {expr1} else {expr2};

Of course, one can always write

my $var;
if (predicate) ($var = expr1;) else ($var = expr2;)

However, in non-trivial codes where a mere expression is needed in many
places or nested tests, the
latter can get very cumbersome and impractical. (the example above is
only one situation where an expression is needed.)

Readible or not, I find it a necessary evil. Personally, I rather
wish Perl's "if ... then ... else" returns a value so that "?:"
becomes a mere syntax sugar and can be avoided.

Regarding "programing styles" as a matter how one format his code or
syntax varieties, I
think it is a energy drain and should be avoided. Instead, spend time
studying the language constructs (aka ideoms), or even better:
algorithms and CS theories.

For large programs, readibility depends on one's knowledge of
programing more than choice of syntax and code formating.

 Xah, xah@best.com
 http://www.best.com/~xah/PageTwo_dir/more.html
 Mountain View, CA, USA


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