On May 15, Paul said:

>
>--- Peter Cornelius <[EMAIL PROTECTED]> wrote:
>> The <FILE> doesn't read into $_.  You can add a 'print $_;' in there
>> to verify it.  I think you're getting confused by the
>>    while(<FILE>){...}
>> structure.  This puts the result of the readline into $_ for you if
>> the <FILE> is the only thing in the '()'.  Otherwise, I believe just
>> calling <FILE> on a line by itself sends that line to the bit bucket.
>
>I think that's right. On a line alone is a void context, but while(){}
>is a scalar context. 

Perl changes certain things at compile-time.  If you have a while loop
that can be optimized to 'while (<FH>)', then Perl turns that into 'while
(defined($_ = <FH>))' (and likewise for 'while ($x = <FH>)').

Watch and see:

  friday:~ $ perl -MO=Deparse
  while (<FH>) { 1 }
  while ($x = <FH>) { 1 }
  while (1 and <FH>) { 1 }
  while (1 and $x = <FH>) { 1 }
  while ($x and <FH>) { 1 }
  while ($x and $y = <FH>) { 1 }

  - syntax OK

  while (defined($_ = <FH>)) {
    '???';
  }
  while (defined($x = <FH>)) {
    '???';
  }
  while (defined($_ = <FH>)) {
    '???';
  }
  while (defined($x = <FH>)) {
    '???';
  }
  while ($x and <FH>) {
    '???';
  }
  while ($x and $y = <FH>) {
    '???';
  }

Those last two can't be optimized to a simple assignment from <FH>, so
Perl doesn't do its magical change.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734

Reply via email to