A Modern Perl Success Story for the Internet CLI

| 8 Comments

I've never believed the argument that web applications will completely replace native client-side applications. The last time someone said that, I held up my Android phone and said "This is a pervasive Internet device. I use several applications regularly. The only native web application I use is a web browser."

Maybe I'm an old dinosaur well before my time, but I'm happy writing these posts in Vim (with copious macros and customizations) through It's All Text!. I don't mind using Git from the command line, and I think ctags is still one of the most useful IDE inventions.

That doesn't mean I reject the billion or so devices that make up the Internet, nor all of the information they contain, nor the services they provide.

It means I can be clever about using them.

A Modern Perl Identi.ca/Twitter CLI

Twitter may be a morass of uselessness which all too often proves the existence of sewers upstream of the collective stream of consciousness, but in between all of the "lollers" and "RT RT RT lollers cat falling down video!!" sometimes it's a good way to pass on lightweight, transient information to people who might care.

I've used Identi.ca for a year now. As part of marketing my business, I set up a couple of accounts to which I publish notifications about the fiction and non-fiction publishing work we do. If I edit a chapter in an upcoming novel we've announced, I can send out a short notice to anyone following that novel's progress. The same goes for a book.

It's easy, it's quick, it's non-intrusive, and people really do seem to follow this information. (Wouldn't you like to know how far away is the next book from your favorite author? Still waiting for that seventh Zelazny Amber story....)

Setting all of this up for multiple books and multiple accounts could be tricky. I could log into Identi.ca manually every time I want to post an update (maybe a couple of times a day). I have to manage several user names and passwords. What could be a 15-second update when it's fresh in my mind could take several times that long, and I wouldn't do it.

That's why I have a decent shell and modern Perl.

I wrote a proof of concept in ten minutes using Net::Identica; all hail Perl and the author:

#!/usr/bin/perl

use Modern::Perl;
use Net::Identica;
use File::Temp 'tempfile';

sub main
{
    my ($username, $pass, $message) = @ARGV;

    do
    {
        $message = get_message( $message );
    } until ($message && length( $message ) <= 140);

    post( $username, $pass, $message );
}

sub get_message
{
    my $message = shift;

    my ($fh, $filename) = tempfile();
    print {$fh} $message if $message;

    system( $ENV{EDITOR}, $filename );

    seek $fh, 0, 0;
    exit if eof $fh;

    return do { local $/ = <$fh> };
}

sub post
{
    my ($username, $pass, $message) = @_;

    say "[$message]";

    my $ni      = Net::Identica->new(
        legacy   => 0,
        username => $username,
        password => $pass,
    );

    $ni->update( $message );
}

main();

I've highlighted the most interesting line. The program takes three arguments, a username, a password, and an optional message to post. The emboldened line launches (for me) Vim on a temporary file containing the message. If there's no message, I can write one. If there is a message, I can edit it. When I save the file, the program immediately posts it to Identi.ca, with the given username and password.

It's easy to create bash shell aliases to customize the behavior further. I have a file called ~/.alias, which can contain, for example:

alias gigdent="perl ~/bin/dentit gigapolis password"
alias mpdent="perl ~/bin/dentit modern_perl password"

For every new project I start, I can create a new account and shell alias. Then I only have to remember the name of the alias to write a status update.

50 generously-spaced lines of modern Perl code and a bit of shell glue later, I can get my work done with little fuss and even less cognitive overhead from task-switching. Maybe it's not jaw-droppingly amazing like rounded corners on a Web 2.0 startup written by skinny-jeans hipsters with deliberately messy hair and flipflops, but it helps gets my work done and it took longer to write this post than to write the code.

I'll call that a Perl success story any day.

8 Comments

Wait, was there a sixth Amber short story? I only remember reading five. (And seem to recall that was of a planned ten stories which would together make up the next Amber book?)

Nice script, too...

@Sol, Zelazny wrote another story for one of the final Amber magazines. It's difficult to find; I've never seen it. In theory it'll be in an omnibus published sometime.

i'm a little surprised not to see 'package My::Util::Dentit;' and 'main() unless caller;' in this code. sure, it's a prototype, but these changes add only one line of code, and increase testability exponentially.

was this omission intentional?
~particle

@particle, the program's just short enough I don't worry about testing it formally. If I did more with it, I'd definitely do what you say.

In June I wrote a similar script that works that works the other way around. This way you can keep a draft of twitter messages in a text file. You can find it at http://peterstuifzand.nl/a-commandline-script-for-twitter.html
My script can easily be adapted to use Identica.

chromatic, could you say a few words about the file slurping idiom you are using? Specifically, I've never seen $/ not set to undef, but instead set to the value of filehandle.

"Maybe it's not jaw-droppingly amazing like rounded corners on a Web 2.0 startup written by skinny-jeans hipsters with deliberately messy hair and flipflops, but it helps gets my work done and it took longer to write this post than to write the code."

Hilarious. :-)

"I'll call that a Perl success story any day."

A *Modern* Perl success story, even. ;-)

Phillip.

Hi,

What a nice code you done here, i liked!

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

sponsored by the How to Make a Smoothie guide

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on August 5, 2009 4:44 PM.

Reduce Complexity, Prevent Bugs was the previous entry in this blog.

The Whipupitude-Neophyte Conundrum is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Powered by the Perl programming language

what is programming?