Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Forcing array context on a file handle

by fishmonger (Chaplain)
on Oct 30, 2012 at 21:11 UTC ( #1001557=note: print w/replies, xml ) Need Help??

in reply to Forcing array context on a file handle

Did you check perldoc? This is a FAQ

perldoc -q "How do I count the number of lines in a file"

  • Comment on Re: Forcing array context on a file handle

Replies are listed 'Best First'.
Re^2: Forcing array context on a file handle
by perl_walker (Novice) on Oct 31, 2012 at 07:15 UTC

    Hi, ronmrdechai

    $lines = grep {$_} <$file>;
      That works for files in the default case, because every line will have a newline or somesuch, and will not be an empty string. For counting, map would be more appropriate than grep. But tobyink showed the idiom above:
      my $lines =()= <$file>;

      Quantum Mechanics: The dreams stuff is made of

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001557]
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)
[Eily]: oh, boolean context uses the "" overload if no bool overload has been defined
[Eily]: (I wondered if overloading bool was actually necessary)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-07-27 13:46 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (414 votes). Check out past polls.