(続編)Twitterに投稿するelisp + Perl Script

 昨日のは(特にPerlの方が)アレ過ぎたので、頑張って書き直しました。

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
use Net::Twitter;

{

    package Net::Twitter;

    sub update {
        my ( $self, $status ) = @_;

        $status = $self->_format_message($status);
        my $req = $self->{ua}
            ->post( $self->{apiurl} . "/update.json", [ status => $status ] );
        return ( $req->is_success )
            ? JSON::Any->jsonToObj( $req->content )
            : undef;
    }

    sub _format_message {
        my ( $self, $message ) = @_;

        $message =~ s/([!-~]+)/ $1 /g;
        $message =~ s/(\A\s|\s\z)//g;
        $message =~ s/(\s+)/ /g;
        return $message;
    }

}

my $username;
my $password;

my $update;
my $friends;
my $friends_timeline;
my $public_timeline;
my $followers;

my $message = "";

GetOptions(
    'user|u=s'      => \$username,
    'pass|p=s'      => \$password,
    'update'        => \$update,
    'friends'       => \$friends,
    'ftimeline'     => \$friends_timeline,
    'ptimeline'     => \$public_timeline,
    'followers'     => \$followers,
    'msg|message=s' => \$message,
);

die "Unable To Login!!" unless ( $username && $password );

my $twitter = Net::Twitter->new(
    username => $username,
    password => $password
);

my $result;
if ( $update ) {
    die "Message is Empty..." if $message eq "";
    die "Message is 140 byte over..." if length($message) > 140;
    $result = $twitter->update($message);
    print "'" .$result->{text} . "' is posted.\n";
}

 すいません、今度はメソッドの再定義とかいうアホなことしてしまいました。その前に文章にフィルタをかければいい話なんですけどね。やってみたかっただけですごめんなさい。
 正規表現で[!-~]というのを使ってますが、こうすれば、記号も含めて1バイト文字(列)が一気にチェックできます。下手に\wを使うとややこしくなるので、ASCIIテーブルとにらめっこして決めました。でもひょっとしたら、こんなことしなくてもできたかもしれない。
 まだ日本語のみの文章でエラーになる条件がわかってないので、そっちの対処はしてません。一応MeCabを使うことを考えていて、たとえば「名詞の後に助詞」という時に間にスペースを挿入するとか、動詞の前(or後ろ)に入れる、そんなあたりが妥協点ではないかと。どうでもいいけど、最近メカブを初めて食べたんですが、結構うまいっすね。食感は微妙ですが。
 あ、あとはelisp側も微妙に変更されているので。

(defun twitter-post ()
  "Post message from Emacs to Twitter"
  (interactive)
  (if (not twitter-userid)
      (setq twitter-userid (read-from-minibuffer "Twitter UserID?: ")))
  
  (if (not twitter-passwd)
      (setq twitter-passwd (read-from-minibuffer "Twitter Password?: ")))

  (if (not twitter-plpath)
      (setq twitter-plpath (read-from-minibuffer "script path?: ")))

  (let ((msg (read-from-minibuffer "POST to Twitter!: ")))
    (start-process "twitter-proc"              ; プロセス名
                   "tw-post"                             ; バッファ名
                   twitter-plpath                      ; コマンド
                   "--user" twitter-userid         ; ↓以下、オプション
                   "--pass" twitter-passwd
                   "--message" msg
                   "--update"))
  )

(provide 'twitter)

(4/17 追記)
 村瀬さんに指摘されたので、修正。

die "Unable To Login!!" unless ( $username || $password );

die "Unable To Login!!" unless ( $username && $password );

 うーん、unlessの挙動を禿げしく勘違いしてた。はげ恥ついでに、以下のプログラムも貼っておこう。絶対忘れるし。

#!/usr/bin/perl

#unless_test.pl

use strict;
use warnings;

my ( $a1, $a2 );

unless( $a1 || $a2 ) {
    print "1-1 : caught.\n";
}
unless( $a1 && $a2 ) {
    print "1-2 : caught.\n";
}

$a1 = "test";

unless( $a1 || $a2 ) {
    print "2-1 : caught.\n";
}
unless( $a1 && $a2 ) {
    print "2-2 : caught.\n";
}

$a2 = "test2";

unless( $a1 || $a2 ) {
    print "3-1 : caught.\n";
}
unless( $a1 && $a2 ) {
    print "3-2 : caught.\n";
}
taiyoh@basixbox % ./workspace/perl/unless_test.pl
1-1 : caught.
1-2 : caught.
2-2 : caught.