This probably works reasonably well, given that you would expect $bw->readline to return undef after being closed, but a quick check of its docs on CPAN didn't turn up anything actually saying that it will. You're also wastefully checking $found on each iteration, even though there is nothing in the code you posted which ever sets it, aside from its initialization to 0 when declared.while( !$found && defined( my $log_line = $bw->readline ) ) { if ($log_line =~ /GET \/myapp\//) { $last_line = $log_line; $bw->close(); } }
To ensure that neither of these is causing problems, I would rewrite the loop as
This modification skips testing $found, explicitly exits the loop once it finds a match, and will always close $bw whether the file matches or not.while(defined( my $log_line = $bw->readline ) ) { if ($log_line =~ /GET \/myapp\//) { $last_line = $log_line; last; } } $bw->close();
Within the loop itself, you can probably improve performance a fair bit by replacing the regex match with a call to substr, since you're looking for a static string and don't need the overhead introduced by the regex engine.
If it's still slow after all that, try setting up a second loop that does the same thing, but reading the file normally from start to end and storing only the last match. Comparing the time taken by the two versions could provide some clues as to where the problem is. (i.e., If they both take just as long, then File::ReadBackwards isn't doing its job very well.)
In reply to Re: Emulating command line pipe
by dsheroh
in thread Emulating command line pipe
by davistv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |