Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Sunday November 10, 2002
05:33 PM

Read the journals, save the pain

[ #8863 ]

I have been reading most of the journals in use.perl lately, and the experience is rather frustrating. For me, the blogging experience is amazingly tedious. I do not mind writing every day, but the web interfaces and crappy local tools seem like they were invented with people who do not mind computers that get in their way.

No more! The use.perl journal system has been SOAP-aware for a while, even if I have not been. Chris Nandor added SOAP support around March, and Joe Johnston recently showed everyone how to post journals from emacs (and this week I want to port that to BBEdit). That is great, but I do not want to click or point more than once to read all of the journals I have not read yet. I also do not want to spend any time waiting for journal entry to make it across the network.

I wrote a simple program to fetch all of the entries since the last time I read use.perl. I started simply, but then figured that I could use this in all sorts of time-saving ways so I started to generalize it.

I threw in Text::Template so I could control the presentation in some other way and in multiple ways from the same program. Many people have written templating systems, and most of them are too complicated for me. I do not want to set up all sorts of things. Text::Template is low-tech, flexible, and easy-to-use. It is certainly one of my favorite modules nowadays. Thanks Dominus! (When is the last time you thanked the author of your favorite module?).

I started with Tie::MLDBM for more flexibility. I will probably start shoving more info into that structure if I decide I really should have a local copy of entry. Already I have wanted to search through entries while I have done something away from a network connection. For now, Tie::MLDBM is overkill because I have no M in my L.

I make heavy use of environment variables rather than command line switches. If I translate this to mod_perl, I can set the things I need to set with PerlSetEnv directives, and each URL which ultimately uses this can have its own environment. I love mod_perl. Thanks Doug!

#!/usr/bin/perl -w
use strict;
use File::Spec::Functions;
use SOAP::Lite;
use Text::Template;
use Tie::MLDBM;
my $Debug       = $ENV{USE_PERL_JOURNAL_DEBUG}    || 0;
my $Template    = $ENV{USE_PERL_JOURNAL_TEMPLATE} ||
    catfile( $ENV{HOME}, ".journal.tmpl" );
my $Counter     = $ENV{USE_PERL_JOURNAL_COUNTER}  ||
    catfile( $ENV{HOME}, ".journals" );
die "Could not open template\n" unless -r $Template;
my $host        = '';
my $uri         = "http://$host/Slash/Journal/SOAP";
my $proxy       = "http://$host/";
dbmopen my %hash, $Counter, 0640 or die $!;
my $id          = $ENV{USE_PERL_JOURNAL_START} || $hash{next_id}
    || die "$0: I need to know where to start!";
my $start_id    = $id;
print STDERR "$0: Starting at ID [$id]\n" if $Debug;
my $journal  = SOAP::Lite->uri( $uri )->proxy( $proxy );
my $template = Text::Template->new( SOURCE => $Template );
while( my $hash  = $journal->get_entry( $id )->result )
    print $template->fill_in( HASH => { entry => $hash } );
    $hash{next_id} = ++$id;
print "No recent journals\n" if $id == $start_id;
print STDERR "$0: Ending at ID [$id]\n" if $Debug;

Of course, I have to make this work from Mac OS X. My favorite editor, BBEdit, which I gladly pay for and do not care about the source code (mostly), has a command line tool. I can pipe input to it and the text shows up in a BBEdit window. I start the process in the background because SOAP thingys tend to be slow. I am not going to wait for this to finish---a window with all of the entries will pop up when it is done. Anything I print to standard error still shows up in the terminal window.

% perl | bbedit &

Now I can read the latest entries without a web browser, without clicking on things, and without waiting. If I decide I want to respond to something, I can switch to a web browser, but the added work probably protects me from myself by discouraging me from saying stupid things.

Long live plain text!