John W. Krahn am Samstag, 3. Dezember 2005 15.27:
> John Doe wrote:
> > The Ghost am Freitag, 2. Dezember 2005 19.30:
> >> print "$_: ";
> >> my @lines=<FILE>;
> >
> > and close opened files:
> >
> > close FILE or die "couldn't close $File::Find::name: $!";
> >
> >> print "$#lines\n";
> >> $totalLines+=$#lines; #wanted's value is ignored so we have to
> >>do this here.
> >> return;}
> >> print "$totalLines\n";
>
> You bring up an interesting point about closing the filehandle because
> normally you don't have to worry about that as perl will do the right
> thing.
Not sure if I understand you correctly:
Do you suggest *not* to close filehandles, because it's done by perl "doing
the right thing"?
Or should one decide in every case, if closing should be explicitly done or
not?
My thought was: When I always close filehandles, I don't have to think about
closing or not closing them (comparable to give always signs when driving,
even if nobody else is on the road).
For exemple perldoc -f open states:
"[...] You don't have to close FILEHANDLE if you are immediately
going to do another "open" on it [...]"
In a normal case, there is one point at which any filehandle is not reopened:
After the last reopening. So this case would have to be checked in a loop
(pseudocode: close FH if finished reopening)?
> However in the example I posted using the $. variable:
>
> sub wanted {
> ...
> () = <FILE>;
> print "$.\n";
> $totalLines += $.;
> }
> print "$totalLines\n";
>
> Produces an incorrect value for $totalLines unless you close the filehandle
I did not see this point.
> but if you don't close the filehandle then you can do this:
>
> sub wanted {
> ...
> () = <FILE>;
> }
> print "$.\n";
Sorry if it's just a misunderstanding by me wasting your time!
joe
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>