Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Different behaviors between "while" and "map"

by Joost (Canon)
on Dec 06, 2008 at 00:43 UTC ( [id://728442]=note: print w/replies, xml ) Need Help??


in reply to Re: Different behaviors between "while" and "map"
in thread Different behaviors between "while" and "map"

Just to expand a little on this: map, grep, foreach and most while constructs all work on perl's concept of lists.

while (<SOME_FILE_HANDLE>)
is special. it does not act like any other iteration construct, not even like any other while() construct. It explicitly iterates over each record ("line") one by one instead of converting the whole file to a list first.

Replies are listed 'Best First'.
Re^3: Different behaviors between "while" and "map"
by betterworld (Curate) on Dec 06, 2008 at 01:42 UTC

    It helps to know that the loop is expanded to:

    while ($_ = <SOME_FILE_HANDLE>){ ... }

    This shows that <...> is nothing more than a loop condition: the loop body is executed immediately after each evaluation of the <...> operator. This is how a while loop works.

      Not for the last 12 years or so. It's expanded to
      while (defined ($_ = <SOME_FILE_HANDLE>)) { ... }

        Technically, you're both right, because

        while ($_ = <SOME_FILE_HANDLE>){ ... }
        is expanded to
        while (defined ($_ = <SOME_FILE_HANDLE>)) { ... }

        But all this is irrelevant. It's not the magical expansion of <> that causes the scalar context.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found