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.