The Simplest Thing That Could Possibly Work
It should go without saying that when you are writing a book, you need to know how long the chapters are. I'm writing mine in vim, with various customizations to fit the needs of the book. However, I then need to convert my book into MS Word format. As I'm using Word 2008 for Mac, and as its the only version that does not have a scripting language built-in, formatting my plain text to the publisher's requirements is a long, tedious process. There are different headings, code, sample exercises, tables, images, "Try it out" sections, and many tiny, tiny details that need to be formatted correctly to ensure the book can be laid our properly.
In order that I can properly estimate how many pages I've written, the publisher provides an AppleScript program that is 2,204 lines long. You start it running, answer a few questions, and then leave and do something else for a while. It runs over your entire chapter, analyzes the various styles, notes issues that may occur, and after a few minutes, it comes back with an estimated page count.
I wrote a Perl script to list the "Table of Contents" sections for a given chapter. This script also estimates the page count. Here's the code I wrote to do it:
printf "Estimated page count is %d\n" => int( $lines / 45 );
It's rarely more than 2 pages off and that's close enough for my needs.