Frenemies part 2: What a difference a (Perl) module makes

Last week I explored using the Inline::Perl5 mod­ule to port a short Perl script to Raku while still keep­ing its Perl depen­den­cies. Over at the Dev.to com­mu­ni­ty, Dave Cross point­ed out that I could get a bit more bang for my buck by let­ting his Feed::Find do the heavy lift­ing instead of WWW::Mechanizes more general-​purpose parsing.

A lit­tle more MetaCPAN inves­ti­ga­tion yield­ed XML::Feed, also main­tained by Dave, and it had the added ben­e­fit of obvi­at­ing my need for XML::RSS by not only dis­cov­er­ing feeds but also retriev­ing and pars­ing them. It also han­dles the Atom syn­di­ca­tion for­mat as well as RSS (hi dax­im!). Putting it all togeth­er pro­duces the fol­low­ing much short­er and clear­er Perl:

#!/usr/bin/env perl

use v5.12; # for strict and say
use warnings;
use XML::Feed;
use URI;

my $url = shift @ARGV || 'https://phoenixtrap-com.us.stackstaging.com';

my @feeds = XML::Feed->find_feeds($url);
my $feed  = XML::Feed->parse( URI->new( $feeds[0] ) )
    or die "Couldn't find a feed at $url\n";

binmode STDOUT, ':encoding(UTF-8)';
say $_->title, "\t", $_->link for $feed->entries;

And here’s the Raku version:

#!/usr/bin/env raku

use XML::Feed:from<Perl5>;
use URI:from<Perl5>;

sub MAIN($url = 'https://phoenixtrap-com.us.stackstaging.com') {
    my @feeds = XML::Feed.find_feeds($url);
    my $feed  = XML::Feed.parse( URI.new( @feeds.first ) )
        or exit note "Couldn't find a feed at $url";

    put .title, "\t", .link for $feed.entries;
}

It’s even clos­er to Perl now, though it’s using the first rou­tine rather than sub­script­ing the @feeds array and leav­ing off the the $_ vari­able name when call­ing meth­ods on it—less punc­tu­a­tion noise often aids read­abil­i­ty. I also took a sug­gest­ed exit idiom from Raku devel­op­er Elizabeth Mattijsen on Reddit to sim­pli­fy the con­tor­tions I was going through to exit with a sim­ple mes­sage and error code.

There are a cou­ple of lessons here:

  • A lit­tle more effort in mod­ule shop­ping pays div­i­dends in sim­pler code.
  • Get feed­back from far and wide to help improve your code. If it’s for work and you can’t release as open-​source, make sure your code review process cov­ers read­abil­i­ty and maintainability.

Discover more from The Phoenix Trap

Subscribe to get the latest posts sent to your email.

Mark Gardner Avatar

Hi, I’m Mark.

Hi, I’m Mark Gard­ner, and this is my personal blog. I show software developers how to level up by building production-ready things that work. Clear code, real projects, lessons learned.

Comments

5 responses to “Frenemies part 2: What a difference a (Perl) module makes”

  1. […] Frenemies part 2: What a dif­fer­ence a (Perl) mod­ule makes […]

  2. sigzero Avatar

    I am chuck­ling at the pic.

  3. Mark J. Reed Avatar
    Mark J. Reed

    For short pro­grams with no oth­er sub­rou­tines, I pre­fer using unit sub MAIN(signature here);, which treats the entire file as the body of the MAIN sub­rou­tine and saves you some curlies and an inden­ta­tion lev­el while still get­ting you the auto­mat­ic argu­ment pars­ing that comes from declar­ing a MAIN sub.

  4. Mark Gardner Avatar

    @natalie #Perl to #RakuLang isn’t nec­es­sar­i­ly an upgrade,” it’s a port to a dif­fer­ent ecosys­tem and lan­guage with some similarities.I did a pair of blog posts last year show­ing how you could start with a small bit of Perl code and then rewrite some of it in Raku while still using Perl CPAN mod­ules:• https://phoenixtrap-com.us.stackstaging.com/2021/08/17/perl-raku-best-frenemies/https://phoenixtrap-com.us.stackstaging.com/2021/08/24/frenemies-part-2-what-a-difference-a-perl-module-makes/
    perl
    raku­lang
    Perl & Raku: Best frenemies—The Phoenix Trap

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)