On Mon, Jan 21, 2002 at 12:50:38PM -0800, Larry Wall wrote:
> In most other languages, you wouldn't even have the opportunity to put
> a declaration into the conditional.  You'd have to say something like:
> 
>     my $line = <$in>;
>     if $line ne "" { ... }
> 
> Since
> 
>     if my $line = <$in> { ... }
> 
> is Perl shorthand for those two lines, I don't see how one can say that
> the variable is more related to the inside than the outside of the block.
> One can claim that the code after the C<if> may not be interested in
> C<$line>, but the same is true of the block itself!  The conditional
> only decides whether the block runs.  It's not part of the block.
> 
> Larry

Particularly in perl, boolean values are often very interesting. I
very much like the "curly brackets are the only things that delimit
scopes within a file, dammit" rule, but I also like to use very
localized conditionals:

   if (my $result = expensive_computation()) {
           ...do stuff with $result...
   }

   if (my $result = expensive_computation2()) { ... }

   func($result);

I would like for there to be a way of saying that so that the
func($result) would trigger an error about using an unknown variable.
(I do *not* want the above syntax to be that way, though.)

Perhaps:

   if (expensive_computation()) {
        my $result = __HOW_DID_I_GET_HERE__;
        ...do stuff with $result...
   }

I'm just not just how to spell __HOW_DID_I_GET_HERE__.

 - $LAST, for the value of the last executed expression?
 - $__? (ick) 
 - Say, isn't $? going away, because it's getting absorbed by $!

I rather like that last one.

  if (expensive_computation()) {
        my $result = $?;
        ...do stuff with $result...
  }

In fact, it seems clear enough that renaming is only necessary if you
have nested conditionals.

  while (read(FH, $buf, 10)) { print "Not EOF yet, received $? bytes\n"; }

Though I don't know if 

  foo() && print "Yeah baby";

should set it or not. I'd vote not.

Reply via email to