Pete Emerson wrote:

> Mar 16, 2003 at 5:08pm from Rob Dixon:
>
> RD>         (print 'abc') + ($success = 0) + (last);
>
> Rob, thanks, this example helps drive the point home. Would you also argue
> that
>
> open INFILE, $file || die "Can't open $file: $!";
>
> should really be written as
>
> die "Can't open $file: $!" if (!open INFILE, $file);
>
> or is this a different kettle of fish?

I won't speak for Rob, but to my eyes, this is a very different kettle of fish.

What is the primary task when we are opening a file?  Is it whether we should crap out 
and die?  Probably not.  We add the error handler as an addendum, out of prudence, but 
our primary purpose is still to open the file.

With a conditional, our purpose is generally to answer a question about some value, 
and proceed with the main action of our program based on that value.  Conditionality 
in this case is at the forefront.  So we are really discussing three different things 
here:

1)  Situations where we determine the overall flow of execution based on some value or 
set of values.  For this we use explcit conditional structures if, while, and until.

2)  Situations where the choice between two values for a given variable can be 
determined quickly
and clearly on the basis of some concisely-expressed condition.  This is the 
appropriate context for use of the conditinal evaluation operator [lvalue = condition 
? alt_a : alt_b ].

3)  Situations where there good practice calls for us to be prepared for the 
unexpected.  This is the kind of situation you presented above.  when we drive on a 
thoroughfare, we don't engage the barkes at each side street, but if we value our 
lives, we do let up on the gas pedal.  Adding the error-handler to the end is akin to 
this.

I'll note here that the expression as posted is almost never appropriate, and may 
cause serious bugs.  The boolean algebraic operator || is not the same as the 
flow-control command or.  For one thing, the have  very different precedence.  The 
boolean operator has high precede3nce, and is evaluated before any rvalue of which it 
is part is assigned to an lvalue.  The flow-control operator has very low precedence, 
and is evaluated only afeter all commands to its left are executed.

The important thing is for code to reflect as clearly as possible the l;ogic of what 
we are trying to do.
This is why I often encourage people to start out completely aaway from thier code 
editors. It is altogether too tempting, especially given the liberties that Perl 
provides, to get caught up in the clever things we can do with operations and crypotic 
expression.  This might be fun and impressive, but its not good programming.  Good 
programming is defined by clear descriptions of our purpose and process expressed with 
available language structures.

The point is to say, as directly as possible, what we mean.

Joseph



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to