Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: file processing, while and foreach: a n00b experience

by broquaint (Abbot)
on Aug 27, 2003 at 09:09 UTC ( [id://286987]=note: print w/replies, xml ) Need Help??


in reply to file processing, while and foreach: a n00b experience

They're two different things really - foreach takes a list and iterates over the given list, whereas while will keep looping until it's condition expression evaluates to false. In the first instance where you're using foreach, <FH> is evaluated in list context, as foreach evaluates it's provided arguments in list context, which will then read until it reaches an eof and then returns the lines that were read as a list. Whereas when you use a while, perl will auto-magically wrap your filehandle read with a check for definedness e.g
perl -MO=Deparse -e 'while(<>) { }' while (defined($_ = <ARGV>)) { (); } -e syntax OK
Then will continue looping until the filehandle reaches an eof, so only ever reads in a line at a time. See. perlsyn for more info on looping.
HTH

_________
broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://286987]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found