Showing posts with label Perl6. Show all posts
Showing posts with label Perl6. Show all posts

Sunday, February 8, 2009

How to post in both Twitter and Identica at once - Part II

twit.pl: version 0.0.1
In this post you will find the first version of twit.pl.
I have relied a lot on Dave's code with the following changes:
- I removed the IO::Interactive module (I did not see any difference whether I used it or not, so I removed the dependency)
- I used the Getopt module to parse the command line
- I added Identica support

Here is version 0.0.1 of twit.pl.
Use it this way:
perl twit.pl -s "This is my message and I'll stick to it!"

#!/usr/bin/perl
use 5.10.0; #To be able to use "say" function
use strict;
use warnings;
use Getopt::Long; #To parse the command line
use Net::Twitter; #API to twitter.com and identi.ca

my $VERSION = "v0.0.1";
my $TwitUser = 'twitterlogin';
my $TwitPass = 'twitterpasswd';
my $IdenticaUser = 'identicalogin';
my $IdenticaPass = 'identicapasswd';
my $TwitterUse = 1; # 1-> send to twitter, 0 -> do not send
my $IdenticaUse = 1; # 1-> send to identica, 0 -> do not send
my $Status;

#Parse command line arguments
GetOptions ("status=s" => \$Status);
say "Sending: $Status";
#There's a hard limit on the size of twits
if (length $Status > 140) {
say "Too long";
exit;
}
if ($TwitterUse) {
my $twitter = Net::Twitter->new(username => $TwitUser, password => $TwitPass);
if ($twitter->update($Status)) {
say "twitter: OK";
}
else {
say "twitter: FAIL";
}
}
if ($IdenticaUse) {
my $identica = Net::Twitter->new(identica => 1, username => $IdenticaUser, password => $IdenticaPass);
if ($identica->update($Status)) {
say "Identica: OK";
}
else {
say "Identica: FAIL";
}
}

__END__
History:
v0.0.1 (2009/02/08): First version of twit.pl. Use as:
>perl twit.pl -s "I have just finished coding the first version of twit.pl"
New Perl concepts:
- On line 2, use 5.10.0 means that your program explicitly requires a minimal Perl version number (5.10.0 in this case). This is so we can use the "say" function.
say "hello"; is equivalent to print "hello\n";
- Line 6 loads the Net::Twitter module.
- Line 21: length command returns the length of the following scalar.
- Line 23: the exit function terminates the application.
- Line 26 creates an instance of the Net::Twitter class. You can then manipulate the object with the -> operator.
- Line 35 does the same thing but for identi.ca. You just need to set the configuration item identica to true (non zero).
- Lines 27 and 36 send the message to the proper websites. If there is an error, undef is returned. The error return is tested inside the if() statement

How to improve on twit.pl
This script does the job but there are many ways to improve on it. Here's my to-do list:
  • Use template
  • Read Login and password from file (so I don't accidentally divulge my own login, har!)
  • Factorize code (use subroutine to suppress code duplication)
  • Set TwitterUse and IdenticaUse from command line option
  • More error traces: check for no argument, traces when sending msg fails
  • Simple GUI interface (1 text box + 1 check box for each Twitter and Identi.ca + 1 "Send" button)
French expression of the day:
"C'est le premier pas qui compte": The first step is always the hardest (litt: The first step is the one that counts).

Next posts:
  • Improving on twit.pl: fonctions and reading from file
  • How to install and use Google Analytics on your Blogger blog
  • Improving on twit.pl: using more of the Net::Twitter API
  • Improving on twit.pl: Graphical User interface
  • Perl help resources
  • POD

How to post in both Twitter and Identica at once - Part II

twit.pl: version 0.0.1
In this post you will find the first version of twit.pl.
I have relied a lot on Dave's code with the following changes:
- I removed the IO::Interactive module (I did not see any difference whether I used it or not, so I removed the dependency)
- I used the Getopt module to parse the command line
- I added Identica support

Here is version 0.0.1 of twit.pl.
Use it this way:
perl twit.pl -s "This is my message and I'll stick to it!"

#!/usr/bin/perl
use 5.10.0; #To be able to use "say" function
use strict;
use warnings;
use Getopt::Long; #To parse the command line
use Net::Twitter; #API to twitter.com and identi.ca

my $VERSION = "v0.0.1";
my $TwitUser = 'twitterlogin';
my $TwitPass = 'twitterpasswd';
my $IdenticaUser = 'identicalogin';
my $IdenticaPass = 'identicapasswd';
my $TwitterUse = 1; # 1-> send to twitter, 0 -> do not send
my $IdenticaUse = 1; # 1-> send to identica, 0 -> do not send
my $Status;

#Parse command line arguments
GetOptions ("status=s" => \$Status);
say "Sending: $Status";
#There's a hard limit on the size of twits
if (length $Status > 140) {
say "Too long";
exit;
}
if ($TwitterUse) {
my $twitter = Net::Twitter->new(username => $TwitUser, password => $TwitPass);
if ($twitter->update($Status)) {
say "twitter: OK";
}
else {
say "twitter: FAIL";
}
}
if ($IdenticaUse) {
my $identica = Net::Twitter->new(identica => 1, username => $IdenticaUser, password => $IdenticaPass);
if ($identica->update($Status)) {
say "Identica: OK";
}
else {
say "Identica: FAIL";
}
}

__END__
History:
v0.0.1 (2009/02/08): First version of twit.pl. Use as:
>perl twit.pl -s "I have just finished coding the first version of twit.pl"
New Perl concepts:
- On line 2, use 5.10.0 means that your program explicitly requires a minimal Perl version number (5.10.0 in this case). This is so we can use the "say" function.
say "hello"; is equivalent to print "hello\n";
- Line 6 loads the Net::Twitter module.
- Line 21: length command returns the length of the following scalar.
- Line 23: the exit function terminates the application.
- Line 26 creates an instance of the Net::Twitter class. You can then manipulate the object with the -> operator.
- Line 35 does the same thing but for identi.ca. You just need to set the configuration item identica to true (non zero).
- Lines 27 and 36 send the message to the proper websites. If there is an error, undef is returned. The error return is tested inside the if() statement

How to improve on twit.pl
This script does the job but there are many ways to improve on it. Here's my to-do list:
  • Use template
  • Read Login and password from file (so I don't accidentally divulge my own login, har!)
  • Factorize code (use subroutine to suppress code duplication)
  • Set TwitterUse and IdenticaUse from command line option
  • More error traces: check for no argument, traces when sending msg fails
  • Simple GUI interface (1 text box + 1 check box for each Twitter and Identi.ca + 1 "Send" button)
French expression of the day:
"C'est le premier pas qui compte": The first step is always the hardest (litt: The first step is the one that counts).

Next posts:
  • Improving on twit.pl: fonctions and reading from file
  • How to install and use Google Analytics on your Blogger blog
  • Improving on twit.pl: using more of the Net::Twitter API
  • Improving on twit.pl: Graphical User interface
  • Perl help resources
  • POD

Friday, February 6, 2009

How to post in both Twitter and Identica at once

Twitter + Identi.ca in one shot: twit.pl
I recently registered accounts on Twitter and Identi.ca. However, when I want to post a message on both, I have to manually copy and paste each message from a site to the other.
What I propose is to write a little Perl script to automate that process, so that each message that you write is automatically sent to both Twitter and Identi.ca.

I found the base for such a script on Dave Jacoby's blog. He actually did most of the work so there'll be very little change to make (Thanks Dave).
There are some CPAN modules to install (Net::Twitter from Chris Thompson and Modern::Perl from chromatic). That'll be a good example of using the cpan command line in Strawberry Perl.

Install Net::Twitter CPAN module on Strawberry Perl
This CPAN module is the keystone of Dave's script. It is a Perl interface to twitter.com.
To install the module in Strawberry Perl, follow those instructions:
  • Open a DOS shell (cmd.exe)
  • type cpan to open the cpan shell
  • type m Net::Twitter (beware, CPAN module names are case-sensitive) to get help about the module.
  • If it is not installed, you will get a message like this one:
cpan> m Net::Twitter
Fetching with LWP:
http://cpan.strawberryperl.com/authors/id/C/CT/CTHOM/CHECKSUMS
Module id = Net::Twitter
CPAN_USERID CTHOM (Chris Thompson )
CPAN_VERSION 2.06
CPAN_FILE C/CT/CTHOM/Net-Twitter-2.06.tar.gz
UPLOAD_DATE 2009-01-26
INST_FILE (not installed)

  • Type install Net::Twitter to start the install process.
  • If like me, you didn't have it installed, it will also try to add the JSON module dependency. This JSON format looks interesting by the way, please remind me to go check it back later.
  • Ignore all the warnings and chose default options when prompted.
  • If all goes well after the install is finished, you should get the following response to m Net::Twitter:
cpan> m Net::Twitter
Database was generated on Fri, 06 Feb 2009 21:31:03 GMT
Module id = Net::Twitter
CPAN_USERID CTHOM (Chris Thompson )
CPAN_VERSION 2.06
CPAN_FILE C/CT/CTHOM/Net-Twitter-2.06.tar.gz
UPLOAD_DATE 2009-01-26
MANPAGE Net::Twitter - Perl interface to twitter.com
INST_FILE C:\strawberry\perl\site\lib\Net\Twitter.pm
INST_VERSION 2.06

Run the same process for Modern::Perl (current version is 1.02).
I didn't get any problem installing those modules on my machine. Hope all goes smoothly for you also.
I noticed is that ActivePerl was still associated with .pl files (StrawberryPerl was installed after it). You'll need to change that association to c:\strawberry\perl\bin\perl.exe.

French expression of the day:
"une clef de voûte": a keystone

Next posts:
  • twit.pl: using the Net::Twitter module
  • Improving on the Perl template
  • How to install and use Google Analytics on your Blogger blog
  • Perl help resources
  • POD

How to post in both Twitter and Identica at once

Twitter + Identi.ca in one shot: twit.pl
I recently registered accounts on Twitter and Identi.ca. However, when I want to post a message on both, I have to manually copy and paste each message from a site to the other.
What I propose is to write a little Perl script to automate that process, so that each message that you write is automatically sent to both Twitter and Identi.ca.

I found the base for such a script on Dave Jacoby's blog. He actually did most of the work so there'll be very little change to make (Thanks Dave).
There are some CPAN modules to install (Net::Twitter from Chris Thompson and Modern::Perl from chromatic). That'll be a good example of using the cpan command line in Strawberry Perl.

Install Net::Twitter CPAN module on Strawberry Perl
This CPAN module is the keystone of Dave's script. It is a Perl interface to twitter.com.
To install the module in Strawberry Perl, follow those instructions:
  • Open a DOS shell (cmd.exe)
  • type cpan to open the cpan shell
  • type m Net::Twitter (beware, CPAN module names are case-sensitive) to get help about the module.
  • If it is not installed, you will get a message like this one:
cpan> m Net::Twitter
Fetching with LWP:
http://cpan.strawberryperl.com/authors/id/C/CT/CTHOM/CHECKSUMS
Module id = Net::Twitter
CPAN_USERID CTHOM (Chris Thompson )
CPAN_VERSION 2.06
CPAN_FILE C/CT/CTHOM/Net-Twitter-2.06.tar.gz
UPLOAD_DATE 2009-01-26
INST_FILE (not installed)

  • Type install Net::Twitter to start the install process.
  • If like me, you didn't have it installed, it will also try to add the JSON module dependency. This JSON format looks interesting by the way, please remind me to go check it back later.
  • Ignore all the warnings and chose default options when prompted.
  • If all goes well after the install is finished, you should get the following response to m Net::Twitter:
cpan> m Net::Twitter
Database was generated on Fri, 06 Feb 2009 21:31:03 GMT
Module id = Net::Twitter
CPAN_USERID CTHOM (Chris Thompson )
CPAN_VERSION 2.06
CPAN_FILE C/CT/CTHOM/Net-Twitter-2.06.tar.gz
UPLOAD_DATE 2009-01-26
MANPAGE Net::Twitter - Perl interface to twitter.com
INST_FILE C:\strawberry\perl\site\lib\Net\Twitter.pm
INST_VERSION 2.06

Run the same process for Modern::Perl (current version is 1.02).
I didn't get any problem installing those modules on my machine. Hope all goes smoothly for you also.
I noticed is that ActivePerl was still associated with .pl files (StrawberryPerl was installed after it). You'll need to change that association to c:\strawberry\perl\bin\perl.exe.

French expression of the day:
"une clef de voûte": a keystone

Next posts:
  • twit.pl: using the Net::Twitter module
  • Improving on the Perl template
  • How to install and use Google Analytics on your Blogger blog
  • Perl help resources
  • POD