2017 twenty-four merry days of Perl Feed

Around the world with Emoji

Emoji::NationalFlag - 2017-12-02

It was the night before the night before Christmas and, as one of the best programmers on the rapid response team, Sherry Silversparkles was in a rush. She'd been handed the task of automating the updating of Santa's twitter account as he travelled around the world. The kind of updates she'd been asked to post were really simple, as was the code she'd hastily written to do so.

use Net::Twitter;
use Country::Codes qw( country );

sub tweet {
    my $country_code = shift;

    my $country = country($country_code)
        or die "Unknown country!";

    my $twitter = Net::Twitter->new(
        username => $user,
        password => $pass,
        source => 'Santa Twitteromatic Two Thousand',
    );

    $twitter->update("Father Christmas's sleigh is flying to $country");
    return;
}

She submitted her work for acceptance by Elf command. However, within minutes she'd got a report back on the contents of her messages: tl;dr. Apparently today's youths wouldn't read more than a couple of words before getting bored and going back to checking Instagram. Elf command had suggested a three glyph alternative message:

🎅→🇺🇸

"Nothing like changing the specs at the last minute" Sherry mused to herself. Now she'd have to work out how to do these changes in a jiffy! Sherry already knew how to render the emoji for Santa and that right arrow:

print "\N{FATHER CHRISTMAS}\N{RIGHTWARDS ARROW}";

But as hard as she looked in her unicode charts, she couldn't find the character for the US flag, nor for any of the other countries.

After much head scratching, and a relaxing glass of mulled wine, Sherry discovered that not all glyphs in Unicode are represented by a single character. Some glyphs are made up by combining one or more characters:

print "\N{REGIONAL INDICATOR SYMBOL LETTER U}";
print "\N{REGIONAL INDICATOR SYMBOL LETTER S}";

On systems that don't understand how to render flags that would simply display as an odd looking U followed by an odd looking S (which hopefully the end user would know as the ISO 3166-1 code for the United States). On more capable systems that would be turned into a United States flag emoji.

So, that was simple - sort of - but Sherry still needed some way to turn the country code that her subroutine was being passed into those corresponding regional indicator characters. Of course, this being Perl, there was a module for that on the CPAN:

use Emoji::NationalFlag qw( code2flag );

...

$twitter->update(
   "\N{FATHER CHRISTMAS}\N{RIGHTWARDS ARROW}" . code2flag( $country_code )
);

This time Sherry's changes were accepted, leaving her free to move onto the next last-minute Christmas programming panic...

Gravatar Image This article contributed by: Mark Fowler <mark@twoshortplanks.com>