Hack #20. Read Files Backwards
Process the most recent lines of a file first.
Perl's position in system administration is stable and secure, due in no small part to its fast and flexible text-processing abilities. If you need to slice and dice log files, monitor services, and send out messages, you could glue together the perfect combination of shell and command-line utilities, or you could have Perl do it.
Of course, Perl is a general-purpose language and doesn't always provide every tool you might need by default. For example, if you find yourself processing system logs often, you might wish for a way to read files in reverse order, the most recent line first. Sure, you could slurp all of the lines into an array and read the last one, but on a busy system with lots of huge logs, that can be slow and memory-consuming.
Fortunately, there's more than one way to do it.
The Hack
Yes, you could look up perldoc -F -X
and find a file's size and read backwards until you find the appropriate newline and then read forward...but just install File::ReadBackwards
from the CPAN instead.
Suppose you have a server process that continually writes its status to a file. You only care about its current status (at least for now), not its historical data. If its status is up
, everything is happy. If its status is down
, you need to panic and notify everyone, especially if it's 3 a.m. on Boxing Day.
Simulate the program that writes its logs with:
#!/usr/bin/perl use strict; use warnings; use Time::HiRes 'sleep'; ...
Get Perl Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.