On Fri, 25 Aug 2000, Rick Myers wrote:

> On Aug 24, 2000 at 23:15:15 -0500, Ken Williams twiddled the keys to say:
> > [EMAIL PROTECTED] (Rick Myers) wrote:
> > >On Aug 24, 2000 at 01:15:57 -0500, Ken Williams twiddled the keys to say:
> > >> The following patch eliminates a warning during 'make test' about 'Value
> > >> of <HANDLE> construct can be "0";'.  No biggie, but it should be fixed.
> > >> 
> > >> ________________________________________________________________
> > >> --- t/modules/request.t 2000/05/12 03:43:24     1.8
> > >> +++ t/modules/request.t 2000/08/24 06:07:40
> > >> @@ -125,7 +125,7 @@
> > >>      my $lines = 0;
> > >>      local *FH;
> > >>      open FH, $file or die "open $file $!";
> > >> -    ++$lines while (my $dummy = <FH>);
> > >> +    ++$lines while <FH>;
> > >>      close FH;
> > >>      my(@headers);
> > >>      if ($Is_dougm) {
> > >> ________________________________________________________________
> > >
> > >This reverses a previous patch that fixes a fatal 'Modification of a
> > >read-only value attempted at modules/request.t line 128', which returns
> > >with this patch.
> > >
> > >See if this one finds us a happy median...
> > >
> > >--- t/modules/request.t~   Thu Aug 24 18:24:39 2000
> > >+++ t/modules/request.t    Thu Aug 24 18:41:22 2000
> > >@@ -125,7 +125,7 @@
> > >     my $lines = 0;
> > >     local *FH;
> > >     open FH, $file or die "open $file $!";
> > >-    ++$lines while (my $dummy = <FH>);
> > >+    ++$lines while defined <FH>;
> > >     close FH;
> > >     my(@headers);
> > >     if ($Is_dougm) {
> > 
> > 
> > THAT's weird - what in the world is the read-only value that's being
> > modified?  
> 
> It's $_.
> 
> Here's the relevant code from request.t to illustrate...
> 
>   for ( qw(perlfunc.pod perlpod.pod perlxs.pod), @binary) {
>       upload_test($_);
>   }
> 
>   sub upload_test {
>       ++$lines while <FH>;
>   }

The real fix should be either:

  for my $file ( qw(perlfunc.pod perlpod.pod perlxs.pod), @binary) {
      upload_test($file);
  }

  sub upload_test {
      ++$lines while <FH>;
  }

or

  for ( qw(perlfunc.pod perlpod.pod perlxs.pod), @binary) {
      upload_test($_);
  }

  sub upload_test {
      local $_;
      ++$lines while <FH>;
  }

The really one and the only correct one is of course:

  for my $file ( qw(perlfunc.pod perlpod.pod perlxs.pod), @binary) {
      upload_test($file);
  }

  sub upload_test {
      local $_;
      ++$lines while <FH>;
  }

You are trying to workaround the problem. It's always becomes very subtle
to find bug when you do:

  for (1..5) { function($_) }

Because you never know whether the function itself is using $_ and
modifying it. In this situation you are lucky that the list you pass to
the for is readonly -- perl reports a problem. A much worth thing happen
when the variable that you pass to for() is a real array:

  for (@array) { function($_) }

$_ is aliased to the real variables in @array -- it's not a copy!!! So if
function() alters $_, your array gets changed!!! This is a side effect
which can byte you very badly.

Therefore, if you are a good perl programmer and want your code to be
clean, when you write subroutines which work with $_, always start them
with:

  local $_;

If you write for() and similar loops that alias $_ to the real variables
in the list, *always* do:

  for my $val (@array) { foo($val) }

use $_ only if the loop doesn't call any function unless you are sure that
the function declares 'local $_';. The following is a perfect snippet of
code:

  my $count = 0;
  for (@values){
    $count += $_;
  }

> 
> The hint is that `while <FH>' will try to set $_ to the value of each
> line read.
> 
> Rick Myers                            [EMAIL PROTECTED]
> ----------------------------------------------------
> The Feynman Problem       1) Write down the problem.
> Solving Algorithm         2) Think real hard.
>                           3) Write down the answer.
> 



_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://jazzvalley.com
http://singlesheaven.com http://perlmonth.com   perl.org   apache.org



Reply via email to