Readline using foreach and while
- From: Saurabh Jain <hundredrabh@xxxxxxxxx>
- Date: Tue, 25 Mar 2008 00:25:01 -0700 (PDT)
Hi,
Is there any difference in reading a file using a while or a
foreach in perl?
If I do :
foreach(<filehandle>) {
my $local = <filehandle>; # I assumed I will increment the file
descriptor here
print " local $local\n";
}
But if I do :
while(<filehandle>) {
my $local = <filehandle>; # I assumed I will increment the file
descriptor here
print " local $local\n";
}
It works fine....
Is there something wrong or some difference in the two operations? Or
am I missing something?
Thanks and Regards,
Saurabh
Small example to replicate the issue
my file name is test.pl
#!/usr/bin/perl
open (handle,"test.pl")||die "\n $0 Could not open $! \n";
my $line = <handle>;#read a line till \n or eof
print " line $line";
#foreach(<handle>){ # Not as expected
while(<handle>){ # Works as expected
$line =<handle>;#read a line till \n or eof
print " in side $line";
$line =<handle>;#read a line till \n or eof
print " in side $line";
$line =<handle>;#read a line till \n or eof
}
close handle;
.
- Follow-Ups:
- Re: Readline using foreach and while
- From: Jürgen Exner
- Re: Readline using foreach and while
- From: Gunnar Hjalmarsson
- Re: Readline using foreach and while
- From: Ben Bullock
- Re: Readline using foreach and while
- Prev by Date: Re: strategys other than subroutine and OO?
- Next by Date: Re: Perl code to fill-in online form
- Previous by thread: FAQ 7.22 What's the difference between calling a function as &foo and foo()?
- Next by thread: Re: Readline using foreach and while
- Index(es):
Relevant Pages
|