Geetha Weerasooriya wrote: > > Dear all, > > When I was reading a Perl code I found the following line. Can u > please explain what it means? > > !defined($rt_nearest) or $dh<$dist or next;
Hi Geetha Oh dear, it's not very readable is it! I assume you know what 'next' does. If not, look at perldoc -f next. The statement uses what is called the 'short-circuit' behaviour of the or operator. perldoc perlop says this: :: Binary "||" performs a short-circuit logical OR operation. That is, :: if the left operand is true, the right operand is not even evaluated. :: Scalar or list context propagates down to the right operand if it is :: evaluated. So if $rt_nearest is defined then nothing else happens. If it is undefined but $dh < $dist then again nothing happens. If both are false then the next is executed to go to the next iteration of the loop. It's the same as: next unless !defined($rt_nearest) or $dh<$dist; or, more clearly next if defined($rt_nearest) and $dist < $dh; which is how it should have been written. It's now clear that the loop is looking for the 'nearest' of a list of objects. If an object has already been found ($rt_nearest is defined) and its distance is less than that of the current object ($dist < $dh) then go to the next in the list. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
