in reply to Line by line buffered read

I usually use something like:

use strict; use warnings; open (IN,"<file"); my $line; while($line=<IN>){ do_something_with_line(); } close(IN);

probably nobody advised you to read "How do I post a question effectively?", so I do, especially about "Use strict and warnings".

Replies are listed 'Best First'.
Re^2: Line by line buffered read
by dasgar (Priest) on Aug 20, 2010 at 16:47 UTC

    That's kind of what I was thinking of, but I would prefer to modify the open statement to include a die statement, such as below:

    open (IN,"<",$file) || die "Unable to open file '$file': $!\n";

    Also, I would think that the sleep statements in the OP are actually "slowing" it down by making it run longer. Based on the code provided, it doesn't look like the sleep statements are needed. Of course, since I have never used sysread, I may be completely wrong about this.

      Thansk for de "or die", the sleep is just for show best the ouptput each time i print, in a big file, else, u can't show it instead.
Re^2: Line by line buffered read
by muyprofesional (Initiate) on Aug 20, 2010 at 16:58 UTC
    Thanks. I need buffered read for speed.

      You're not making any sense. Line by line reading (while (<>)) is buffered. sysread, on the other hand, provides no buffering.

      If you want to provide your own buffering instead of using Perl's, you could do

      my $buf = ''; for (;;) { my $rv = sysread($fh, $buf, BLOCK_SIZE, length($buf)); die("sysread: $!") if !defined($rv); last if !$rv; process_line($1) while s/^([^\n]*\n)//; } process_line($buf) if length($buf);

      Update: Fixed problem mentioned by ibm1620 in comment.

        Please check that regular expression again :) (to be exact, the substitution expression.)