Last weekI explored using the Inline::Perl5module to port a short Perlscript to Rakuwhile still keeping its Perl dependencies. Over at the Dev.to community, Dave Cross pointed outthat I could get a bit more bang for my buck by letting his Feed::Finddo the heavy lifting instead of WWW::Mechanize’s more general-purpose parsing.
A little more MetaCPANinvestigation yielded XML::Feed, also maintained by Dave, and it had the added benefit of obviating my need for XML::RSSby not only discovering feeds but also retrieving and parsing them. It also handles the Atom syndication formatas well as RSS (hi daxim!). Putting it all together produces the following much shorter and clearer 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 closer to Perl now, though it’s using the firstroutine rather than subscripting the @feedsarray and leaving off the the $_variable name when calling methods on it—less punctuation noise often aids readability. I also took a suggested exitidiomfrom Raku developer Elizabeth Mattijsenon Reddit to simplify the contortions I was going through to exitwith a simple message and error code.
There are a couple of lessons here:
- A little more effort in module shopping pays dividends in simpler code.
- Get feedback 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 reviewprocess covers readability and maintainability.


Comments
5 responses to “Frenemies part 2: What a difference a (Perl) module makes”
[…] Frenemies part 2: What a difference a (Perl) module makes […]
I am chuckling at the pic.
For short programs with no other subroutines, I prefer using
unit sub MAIN(signature here);, which treats the entire file as the body of the MAIN subroutine and saves you some curlies and an indentation level while still getting you the automatic argument parsing that comes from declaring a MAIN sub.unit subis not documented underunit. It is, however, under “Command line interface”. 🙁@natalie #Perlto #RakuLangisn’t necessarily an “upgrade,” it’s a port to a different ecosystem and language with some similarities.I did a pair of blog posts last year showing how you could start with a small bit of Perl code and then rewrite some of it in Raku while still using Perl CPAN modules:• 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
rakulang
Perl & Raku: Best frenemies—The Phoenix Trap