昨日作っていた, コマンドラインからtwitterをつぶやける君が完成しました.
#!/usr/bin/env perl use strict; use warnings; use Config::PL; use Encode; use File::Spec::Functions qw/catfile/; use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help/; use Net::Twitter::Lite::WithAPIv1_1; my $twitter_keys = config_do catfile($ENV{HOME}, '.twinchan.rc'); my $nt = Net::Twitter::Lite::WithAPIv1_1->new( consumer_key => $twitter_keys->{consumer_key}, consumer_secret => $twitter_keys->{consumer_secret}, access_token => $twitter_keys->{access_token}, access_token_secret => $twitter_keys->{access_token_secret}, ); my ($name, $location); GetOptions( "name|n=s" => \$name, "location|l=s" => \$location, ); my $profile = {}; $profile->{name} = $name if $name; $profile->{location} = $location if $location; if (keys $profile) { print "[submit?]\n"; for my $key (keys %$profile) { print "$key: $profile->{$key}\n"; $profile->{$key} = decode_utf8($profile->{$key}); } print "[y/n] > "; chomp(my $input = <STDIN>); if ($input =~ /^[yY]/) { my $result = $nt->update_profile($profile); print "-> complete.\n"; } else { print "-> cancel.\n"; } } if ($ARGV[0]) { my $tweet = decode_utf8($ARGV[0]); my $tweet_length = length $tweet; if ($tweet_length > 140) { print "-> over 140 word\n"; } else { print "[submit?]\n"; print "$ARGV[0]\n"; print "[y/n] > "; chomp(my $input = <STDIN>); if ($input =~ /^[yY]/) { my $result = $nt->update($tweet); print "-> complete.\n"; } else { print "-> cancel.\n"; } } } exit;
元々, twitterのプロフィール(名前とか, 場所とか...)を変更するのが面倒なので, コマンドラインから設定できるようにしよう! と思って作り出したので, ツイート機能に加えてプロフィールの変更機能を付けました.
今日の詰まりポイント
ツイートする為の'tweet'メソッドやプロフィールを更新する'update_profile'メソッドに対して, 複数の要素をハッシュで渡したい時は, ハッシュのリファレンスとして渡しましょう.
# ok $nt->update_profile({ name => $name, location => $location, }); # ng $nt->update_profile( name => $name, location => $location, );
お恥ずかしい話ですが, これで30分悩みました...