Plurking in Perl

作者:   發佈於:  

Net-Plurk and AnyEvent-Plurk are fine, but they are sometimes too big for one small purpose.

I use this small piece of code to post to plurk:

package PlurkPoster;
use Object::Tiny qw(username password);
use WWW::Mechanize;
use Try::Tiny;

sub login {
    my $self = shift;
    my $ua = WWW::Mechanize->new;
    $ua->get('http://www.plurk.com/m/login');
    $ua->submit_form(with_fields => { username => $self->username, password => $self->password });
    $self->{ua} = $ua;
}

sub post {
    my ($self, $content) = @_;
    try {
        $self->{ua}->get('/m/');
        $self->{ua}->submit_form(with_fields => { content =>  $content });
    };
}

The usage is simple:

my $plurker = PlurkPoster->new(
    username => $config->{username},
    password => $config->{password},
);
$plurker->login;

$plurker->post(...);
$plurker->post(...);
$plurker->post(...);

This PlurkPoster can only post to plurk, but not reading it. And that is the whole purpose of it.

With Perl, you can always have easy solutions for simple purposes.