Friday, December 05, 2008

Perl vs Java!

I had to write a simple program to send an email when a log file didn't have the right data. This log file is always apppended to - and the last line is what I needed.

That meant reading the file backwards - or perhaps from the end. I wanted to find something really simple in Java to do it. As much as I looked (FileInputStream, FileReader) etc, it all involved copious amounts of work. Since, I didn't know the length of each line, I couldn't easily go to the end of the file, and set an offset!

Here's one of the examples I found: readingfilebackwards for java.

Here's how I did it in perl:
open FILE "$file";
@lines = reverse ;
foreach $line(@lines)
{
do something
}
Simplicity simplicity simplicity!

No comments: