http://www.perlmonks.org?node_id=409972

jplindstrom has asked for the wisdom of the Perl Monks concerning the following question:

How-Perl-saved-the-day today.

The problem: SQL tables and procedures are documented in the source code and now we want to include a subset of them in a written introduction/tutorial. Copying the text into the Word document is bound to make them go out of sync. And it's boring...

The solution: A little script to extract the documentation and write it to small Word files, grouped by sub system. These word files are then linked into the main document, so they can be re-generated independently from the main document.

Making it happen: At first I thought RTF would be the right choice for this , but that just ended up being messy, and I didn't have time to get to know the RTF format. So I turned to automating Word instead. Surprisingly, I couldn't find a simple wrapper around the Win32::OLE module, so I hacked something together using example code from the Net.

The code: Now I have an extremely simplistic module that does the not-so-heavy lifting. This is basically it:

package Word::Document::Writer; use Data::Dumper; use Class::MethodMaker new_with_init => "new", get_set => [ "oWord", "oDocument", "oSelection", "hasWritten" +]; sub init { my $self = shift; my (%hParam) = @_; $self->hasWritten(0); $self->oWord( Win32::OLE->new('Word.Application', 'Quit') ) or die +("Could not create OLE Word: $!\n"); $self->oDocument( $self->oWord->Documents->Add ) or die("Could not + add Word document\n"); $self->oSelection( $self->oWord->Selection ) or die("Could not get + Word selection\n"); return; } sub WriteText { my $self = shift; my ($text, $level) = @_; $level ||= 0; my $style = $level ? "Heading $level" : "Normal"; $self->oSelection->TypeParagraph if($self->hasWritten); $self->hasWritten(1); $self->oSelection->TypeText($text); $self->oSelection->{Style} = $style; return(1); } sub SaveAs { my $self = shift; my ($file) = @_; return( $self->oDocument->SaveAs($file) ); } __END__

I can use it like so:

my $oDocument = Word::Document::Writer->new(); $oDocument->WriteText("A level 1 Heading", 1); $oDocument->WriteText("Basic Normal paragraph"); $oDocument->SaveAs("mydocument.doc");

The new problem: What do I do with this code?

It solves an actual problem, but the problem wasn't very difficult to solve. So it hardly does anything while being very useful in my little script. Is it useful enough to merit a CPAN upload (after adding POD and tests obviously)?

If it is, what namespace should I use? Hogging the namespace for this puny module seems almost rude. While Word::Document::Writer::Simple is an option, most other *::Simple modules are supposed to be simple to use, and a more proper name would be Word::Document::Writer::Simplistic :) But that sounds ridiculous.

So, rude or ridiculous? Or something completely different?

/J