Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: No plan "weakens" my Test scripts?

by Aristotle (Chancellor)
on Feb 19, 2005 at 03:29 UTC ( [id://432628]=note: print w/replies, xml ) Need Help??


in reply to Re: No plan "weakens" my Test scripts?
in thread No plan "weakens" my Test scripts?

my $count = do { my $i; $i++ while <$fh>; $i };
my $count = do { () = <$fh>; $. };

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: No plan "weakens" my Test scripts? (slurps)
by tye (Sage) on Feb 20, 2005 at 03:16 UTC

    I'm pretty sure that () = <$fh> loads the entire file contents into memory at once (on the stack), not throwing any of it away until the entire file has been read. So you might as well just use:

    my $count= ()= <$fh>;

    If you want to avoid memory use:

    my $count= do { 0 while <$fh>; $. };

    - tye        

      I was willing to 'waste' the memory on the $i variable for clarity in my example since the discussion was about testing and not about file line counting. (Why make someone who doesn't know it look up $. just to understand an example?) :-)

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

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

    No recent polls found