hawkeye (hawkeye7) wrote,


Back in the early days of computing, we had a concept of a scripting language, in which you could write simple scripts to perform various chores on the machine.

Over the years I've used a number of them. In general, they are simple, interpretative languages. Anything less would cause me to reach for a real programming language.

For some time now, I've been writing such scripts in Perl. I first started using this at Customs where I had problems with a script that was supposed to FTP a file to Queensland but kept failing without adequate explanation. (It still rarely worked but at least we now knew why.) I used Perl to bring everything under a single control where all the status codes could be checked and reported.

Perl is free and contains a myriad of modules for dealing with various products and packages.

As an example, here is a Perl script I wrote to count the words in a Microsoft Word document. It uses OLE to invoke Word and obtain the count. The code is at the heart of the script that I use to create the reports on thesis progress by chapter.

#!/usr/bin/perl -w
# This script counts the words in a Microsoft Word file
# Usage: wc.pl <file name>
# Author: Ross Mallett 4 November 2004

use Cwd;
use English;
use File::Basename;
use File::Spec;
use Win32::OLE;
use Win32::OLE::Enum;
use strict;

sub words ($) {
   my $count = 0;
   my $enumerate = new Win32::OLE::Enum ($ARG[0]);
    while (defined (my $paragraph = $enumerate->Next ())) {
        my $text = $paragraph->{Range}->{Text};
        $text =~ s/[\r\n]//g;
        $text =~ s/x0b/\n/g;
        my @words = split ' ', $text;
        $count += scalar @words;
    return $count;

my $word = new Win32::OLE ('Word.Application');
Win32::OLE->Option (Warn => 3);

my $file = $ARGV[0];
unless (File::Spec->file_name_is_absolute ($file)) {
    $file = File::Spec->canonpath (cwd) . '\\' . $file;
my $document = Win32::OLE->GetObject ($file);

my $count = words ($document->Paragraphs ());
print "Word count = ", $count, "\n";

$count += words ($document->Footnotes ());
print "Including footnotes = ", $count, "\n";

$word->Quit ();

exit 0;

Tags: perl

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment