Skip to content

Instantly share code, notes, and snippets.

@semifor
Created July 29, 2009 14:53
Show Gist options
  • Save semifor/158200 to your computer and use it in GitHub Desktop.
Save semifor/158200 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Net::Twitter - decoded character vs. encoded byte example
use warnings;
use strict;
use utf8;
# This program produced the following:
#
# http://twitter.com/semifor_test/status/2910290982
# http://twitter.com/semifor_test/status/2910312937
use Net::Twitter;
use Data::Dumper;
use Encode qw/encode_utf8/;
sub tweet_word {
my ($nt, $word) = @_;
my $text = sprintf "Net::Twitter %s using %s string: %s, length %d\n",
Net::Twitter->VERSION,
utf8::is_utf8($word) ? "a decoded character" : "an encoded byte",
$word,
length $word;
my $status = eval { $nt->update($text) };
warn $@ if $@;
print Dumper $status;
}
# in ~/.netrc:
# machine twitter.com
# user YOUR_SCREEN_NAME
# password YOUR_PASSWORD
my $nt = Net::Twitter->new(legacy => 0, netrc => 1);
# Here, we're using utf8 source, so the following string is a decoded character string.
# If we got $word from an external source, we would need to:
#
# $word = decode($encoding, $word);
#
# Where $encoding is the input encoding (perhaps 'UTF-8').
my $word = "einäugig";
tweet_word($nt, $word);
tweet_word($nt, encode_utf8($word));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment