Click here to get back home

Readline using foreach and while

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Readline using foreach and while Saurabh Jain 03-25-2008
Posted by Saurabh Jain on March 25, 2008, 3:25 am
Please log in for more thread options
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;

Posted by Ben Bullock on March 25, 2008, 4:06 am
Please log in for more thread options
> Hi,
> Is there any difference in reading a file using a while or a
> foreach in perl?

The foreach version seems to first read the whole of the file into an
array, and then go through it line by line:

#!/usr/bin/perl
#use warnings;
use strict;
open (handle,"testangleop.pl") or die "$0 Could not open $!";

my $line = <handle>;                #read a line till \n or eof
print "0 line $line";
foreach (<handle>) {                # Not as expected
#while(<handle>){ # Works as expected
print $_;
$line =<handle>;                #read a line till \n or eof
print "1 in side $line";
$line =<handle>;                #read a line till \n or eof
print "2 in side $line";
$line =<handle>;                #read a line till \n or eof
print "3 in side $line";
}

The while seems to increment through the loop.

See also

http://www.unix.org.ua/orelly/perl/prog3/ch02_11.htm

Posted by John W. Krahn on March 25, 2008, 7:59 am
Please log in for more thread options
Ben Bullock wrote:
>> Hi,
>> Is there any difference in reading a file using a while or a
>> foreach in perl?
>
> The foreach version seems to first read the whole of the file into an
> array, and then go through it line by line:

perldoc -q "What is the difference between a list and an array"


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Ben Bullock on March 25, 2008, 11:30 am
Please log in for more thread options
On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:

> Ben Bullock wrote:

>> The foreach version seems to first read the whole of the file into an
>> array, and then go through it line by line:
>
> perldoc -q "What is the difference between a list and an array"

Found in /usr/local/lib/perl5/5.10.0/pod/perlfaq4.pod
What is the difference between a list and an array?

An array has a changeable length. A list does not.

If I had written "the foreach version reads the whole of the file into a
list", I would have contradicted this FAQ entry, which says I can't read
things into a list, because reading things into a list would change the
list's length, and "a list does not" have a changeable length.


Posted by Ben Morrow on March 25, 2008, 12:37 pm
Please log in for more thread options

> On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:
> > Ben Bullock wrote:
>
> >> The foreach version seems to first read the whole of the file into an
> >> array, and then go through it line by line:
> >
> > perldoc -q "What is the difference between a list and an array"
>
> Found in /usr/local/lib/perl5/5.10.0/pod/perlfaq4.pod
> What is the difference between a list and an array?
>
> An array has a changeable length. A list does not.
>
> If I had written "the foreach version reads the whole of the file into a
> list", I would have contradicted this FAQ entry, which says I can't read
> things into a list, because reading things into a list would change the
> list's length, and "a list does not" have a changeable length.

No, you're misunderstanding. A list is immutable *after it has been
created*. Obviously you can create lists with any contents, otherwise
you would be limited to using only lists compiled into perl. foreach
accepts a list as argument and iterates over it; <> in list context
(which is the real problem here) reads the entire file, splits it on
newline (or rather $/), and returns a (newly created) list with the
results. You can't modify the list after that: for an example where you
can, see Tie::File, which reads a file into an *array* instead.

Ben


Similar ThreadsPosted
Asynchronous readline? September 6, 2004, 9:27 am
Term::ReadLine::GNU problem January 1, 2006, 11:57 am
ReadKey/ ReadLine query July 4, 2006, 2:40 am
readline - possible security hole March 30, 2007, 8:50 am
circular references, chdir() and Term::ReadLine::Gnu? February 7, 2006, 8:09 pm
Strange characters using Term::Readline on Win32 July 28, 2006, 9:05 am
HELP: readline() on unopened filehandle DATA at G:/PERLLIB/LIB/site_perl/5.8.2/MIME/WordDecoder.pm line 579. May 21, 2006, 10:59 am
foreach vs. for October 24, 2004, 6:18 pm
foreach in my May 18, 2005, 10:40 am
Using foreach?? maybe.. June 3, 2007, 12:30 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap