Metacpan Download URL

This morning I read that we can now get the download URL for a CPAN module from the Metacpan API! For example, if we visit this URL

$ curl https://api-v1.metacpan.org/download_url/Path::Tiny
{
   "download_url" : "https://cpan.metacpan.org/authors/id/D/DA/DAGOLDEN/Path-Tiny-0.096.tar.gz",
   "version" : "0.096",
   "status" : "latest",
   "date" : "2016-07-03T01:36:29"
}

we get this blob of JSON. If we just want the URL, we could run it through jq

$ curl -s https://api-v1.metacpan.org/download_url/Path::Tiny | jq .download_url
"https://cpan.metacpan.org/authors/id/D/DA/DAGOLDEN/Path-Tiny-0.096.tar.gz"

OALDERS does the same thing in Perl, but it uses three different CPAN modules. HTTP::Tiny is in the standard library, right? Oh, but it needs help from IO::Socket::SSL and Net::SSLeay to get an https URL. And we still need something to encode the URI and something to decode the JSON. Here’s my first crack at it.

#!/usr/bin/env perl

use v5.24;
use warnings;
use HTTP::Tiny;
use JSON;
use URI::Encode qw(uri_encode);

my $module = shift // die "Usage: $0 module\n";

my $uri = uri_encode("https://api-v1.metacpan.org/download_url/$module");

my $res = HTTP::Tiny->new->get($uri);
die "Failed!\n" unless $res->{success};

say decode_json($res->{content})->{download_url};

We didn’t need to use LWP, but we still needed help from CPAN. If we can’t do it with the standard library, why not use Mojolicous? This is a web framework, of course, but it includes some excellent client-side tools too. Here is the same thing using the Mojolicious user agent and JSON decoder.

#!/usr/bin/env perl

use v5.24;
use warnings;
use Mojo::UserAgent;

my $module = shift // die "Usage: $0 module\n";

say Mojo::UserAgent->new
    ->get("https://api-v1.metacpan.org/download_url/$module")
    ->res
    ->json
    ->{download_url};

We can even make it a one-liner using the delightful ojo module!

$ perl -Mojo -E 'say g("https://api-v1.metacpan.org/download_url/".shift)->json->{download_url}' Path::Tiny
https://cpan.metacpan.org/authors/id/D/DA/DAGOLDEN/Path-Tiny-0.096.tar.gz

I’m a little disappointed that it’s not easy to do something as simple as this with just the standard library, but if we use CPAN then we have lots of choices. TMTOWTDI!

Metacpan Download URL

Chalk one up for sed!

Hey, I just saw on @climagic that sed can replace the second occurrence of a regex match with a simple modifier!

$ sed s/i/u/2 <<< fizzbizz
fizzbuzz

Perl’s substitute operator comes from sed and looks pretty much the same, but it can’t do this.

$ perl -pe 's/i/u/2' <<< fizzbizz
Unknown regexp modifier "/2" at -e line 1, at end of line
Execution of -e aborted due to compilation errors.

We’d have to do some awkward counting maneuver with an eval or something.

$ perl -pe 's/i/++$n - 2 ? "i" : "u"/ge' <<< fizzbizz
fizzbuzz

I used to use awk and sed quite a bit, but since learning Perl I find I don’t have much use for them anymore. I miss sed. But here’s one more reason not to let my sed skills atrophy completely.

Chalk one up for sed!

Hello, Stretch!

This weekend I installed Debian 9 (stretch) on my little blue netbook. I love it!

pic of netbook

It has breathed new life into this tired little soldier. I bought this netbook a little over five years ago because I was sick of lugging around the 15 inch laptop (pictured with it) at conferences and whatnot. I had just gone to a conference with my friend Nate, who had a little netbook with a 10 inch screen, when I spotted this little blue guy for like 250 bones. Sold!

I currently also have a 17 inch laptop (Diana’s old one) with Debian 8 (jessie) installed on it and that’s perhaps the nicest operating system I’ve ever used. Until now. Stretch has several nice improvements over jessie.

First, wifi. Getting wifi working remains a bit of a challenge in Linux. When I installed jessie, I did the whole install from CD. Then I hooked up the ethernet cable to get internet access so that I could enable the non-free repository and install the driver I needed for wifi. Only then could I disconnect the ethernet and use the wifi. When I installed stretch, I started the install from a thumb drive (the netbook has no CD) and it stopped me and said I needed non-free driver rtlwifi/rtl8192cfw.bin for wifi and if I had the removable media with it to insert it now. So I went to another computer and grabbed that file off the internet, wrote it to another thumb drive, inserted that into the netbook, and hit “yes”. Voila! I did the rest of the install with wifi— no ethernet required! This seems like a really nice compromise between Debian not wanting to include non-free software and the rest of us wanting to use our wifi. It’s a win-win!

Second, sudo. When I installed jessie, I had a root user and made user tim a sudoer later. After that, I never really used the root account again. When I installed stretch, I made user tim a sudoer just by leaving the root password blank. There is no root account and tim has admin priviledges. Done and done.

Third, perl6. On my jessie box, I did the whole rakudobrew thing. In stretch, there’s a recent version of rakudo all packaged up!

sudo apt install rakudo

Done! Wow!

tim@zog:~$ which perl6
/usr/bin/perl6
tim@zog:~$ perl6 -v
This is Rakudo version 2016.04 built on MoarVM version 2016.04
implementing Perl 6.c.

This is the computer I’ll be taking to YAPC::NA next month, so I had to have that installed. I’ll be taking Damian’s Perl 6 class on Sunday.

Fourth, the printer. In jessie, I had to do a little research to determine that I needed to install system-config-printer and then use it to install my printer. In stretch, this was already there and “Settings -> Print Settings -> Add -> Network Printer” was all it took to discover my wireless printer and to correctly guess and install the driver for it.

During the stretch install, the touchpad of the netbook didn’t work. I was all set to research drivers or something for it, but upon booting the system, the touchpad worked. Still not sure why it didn’t work during the install.

The command-not-found utility still doesn’t work, but just installing it and then changing admin to sudo still fixes it.

I am still tinkering, but I can already tell I’m going to love stretch! I was toying with the idea of getting a fancy new laptop, but now I think I will keep using this little blue netbook for a while longer.

Shiny new 4.5 kernel!

pic of desktop with uname

Hello, Stretch!

Google I/O Extended 2016 Baltimore

pic of screen

Today, I took the day off work and went into town to participate in Google I/O Extended Baltimore. Google I/O, of course, is a huge event out in California with about 7,000 people attending. Google I/O Extended is a series of related events all over the world. The event in Sri Lanka had over 2,000 people. Here in Baltimore, ours was held at AOL/Advertising.com on Brewer’s Hill. Ossum!

pic of mr boh

We had a slate of live talks in the morning:

  • Chris Pino, “Entrepreneurial and Technical Challenges in the Personal Computer Age”
  • Will Gee, BaltiVirtual, “Beyond ‘The Year of VR'”
  • Scott Schopman, OrderUp, “Taking Mobile Testing to the Cloud with Google Cloud Test Lab”
  • Danny Blue, LiveSafe, “Intro to Angular 2”

Following a nice lunch, we watched the Keynote live from California on streaming video.

pic of Google I/O

Afterwards, there were VR demos from BaltiVirtual

pic of BaltiVirtual demo

It was a fun day! If you can’t make it to Google I/O next year, look for the Google I/O Extended event in your area!

Google I/O Extended 2016 Baltimore

Perl 5.24 on a stick

I just upgraded my Perl thumb drive to Strawberry Perl 5.24.0.1!

pic of Perl thumb drive

I simply reformatted it and unzipped the new one onto it

cd /media/tim/Strawberry
unzip ~/Downloads/strawberry-perl-5.24.0.1-64bit-PDL.zip

When I put the thumb drive in a Windows machine, it was mounted as the I: drive, so now I can use Strawberry Perl from cmd.exe

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\tim>I:\perl\bin\perl -E "say qq{Hello, Perl $]}"
Hello, Perl 5.024000

or from PowerShell

PS C:\Users\tim> I:/perl/bin/perl -E "say qq{Hello, Perl $]}"
Hello, Perl 5.024000

Supergood!

Perl 5.24 on a stick

VS Code

One of the things I learned at Go Maryland tonight was that VS Code is not just for Windows; they have versions for Linux and OS X too! With a name like Visual Studio Code, I just assumed it was a Windows thing. Not so!

Naturally, when I got home I had to try it! And here it is running on my Linux machine!

Screen shot of VS Code

As you can see, it understands Go code (on the left), but not Elixir (on the right). At least, not yet. I’m sure it will eventually. I also tried out Perl (yes), Python (yes), and Ruby (yes) — no surprises there — as well as Erlang (no), Pony (no), Rust (yes), and Clojure (yes) — a couple of nice surprises there!

The cursor blinks by default, so the first thing I had to do was figure out how to shut that off 1. It only took me a minute or two to find and change the configuration to a non-blinking cursor. Well done, VS Code!

I doubt I’ll be giving up Emacs any time soon (indeed, I’m typing this blog entry with org2blog), but it’s nice to see another open source editor available. Great job, Microsoft!

Update: In case you’re curious, here’s a shot of the same two files opened in Emacs, which has an Elixir mode.

Screen shot of same two files in Emacs

Footnotes:

1
I can’t stand blinking. I think it’s genetic. My Mom never let us have Christmas lights that blinked either. And to be fair, my beloved Emacs has a blinking cursor by default also
VS Code