Perl Weekly Challenge 165: Line of Best Fit

These are some answers to the Week 165 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on May 22, 2022 at 24:00). This blog post offers some (partial) solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Dancer2 0.301000 Released

On behalf of the Dancer Core Team, version 0.301000 is now available. This is not the release we envisioned; it is missing some things we'd like to have finished, but it does have a couple of new things worth pointing out:

  • A new keyword, request_data, to get the entire deserialized body of the request
  • A new Cookbook recipe for showing how to dynamically enable/disable modules and routes at runtime
  • Numerous doc and bug fixes.

Check out the changelog for a complete list of changes.

The big thing worth pointing out is App::Cmd, which is now not a requirement of Dancer2. A new version of App::Cmd was released with a minimum version requirement of Perl 5.20. We aim to support Dancer2 back to Perl 5.10, which was no longer possible with the current App::Cmd. We had several options to consider in moving forward, and the one we chose was this:

Relatively easy ways to catch memory errors

If you're using XS and C in your Perl module, you might have to worry about memory errors. There are tools like valgrind or memory sanitizer which you can use of course:

valgrind perl -I blib/lib -I blib/arch ./mytestscript

or if your problems are even more serious ones like segmentation fault errors you can run Perl under a debugger like gdb:

gdb perl
% run -I blib/lib -I blib/arch ./mytestscript

However, it might be worth noting some "easy" ways to catch memory errors which actually catch a lot of things.

The first thing is setting the variable to 0 after freeing:

 free (result);
 result = 0;

This prevents you from using the variable again accidentally after freeing, although free (0) is actually not an error, so it doesn't prevent you freeing it twice.

Twenty years ago...

Twenty years ago today I released my first CPAN module: Fork::Queue.

Then there was a group of people supervising module names, and they advised me to move it under the Proc namespace and so it became Proc::Queue.

A couple of weeks before, a bug on one of my scripts had fork-bombed a production box. I barely remember the details now... just that my manager had a serious conversation with me about the incident.

Anyway, that forced me to write the module.

Something I remember for sure is the proud I was of it. How I had been able to replace several Perl builtins playing with the CORE::GLOBAL namespace in a (IMO) clever way!

Perl Weekly Challenge 164: Prime Palindromes and Happy Numbers

These are some answers to the Week 164 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on May 15, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Prime Palindromes

Write a script to find all prime numbers less than 1000, which are also palindromes in base 10. Palindromic numbers are numbers whose digits are the same in reverse. For example, 313 is a palindromic prime, but 337 is not, even though 733 (337 reversed) is also prime.

Prime Palindromes in Raku

We use a data pipeline (chained method invocations) with two grep statements, one to keep palindromes and one to keep prime numbers. This leads to a fairly concise one-line solution:

say (1..^1000).grep({ $_ == .flip }).grep({.is-prime});

Perl weekly challenge 103

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Chinese zodiac

You are given a year $year.

The animal cycle: Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig.

The element cycle: Wood, Fire, Earth, Metal, Water.

Additionally there is a two year cycle between Yin & Yang

This challenge is a relatively simple challenge - and one perl is well suited:

sub year_name {
  return join q( ),
    qw( Yang   Yin                             )[  $_[0]    %  2 ],
    qw( Metal  Water   Wood   Fire  Earth      )[ ($_[0]/2) %  5 ],
    qw( Monkey Rooster Dog    Pig   Rat   Ox
        Tiger  Rabbit  Dragon Snake Horse Goat )[  $_[0]    % 12 ];
}

I failed to pause before blogging

I got this email from PAUSE just now:

Failed: PAUSE indexer report BKB/Go-Tokenize-0.01.tar.gz

     module : switch

It looks like it doesn't like this line of code containing Go keywords:

chan         else         goto         package      switch

A blog post about blog posts

Folk's in the world of Perl have been making amazing efforts to blog more and even to produce video content. That's awesome! Keep it up!

Risking sounding like a mandatory training video from HR, I want to remind budding authors of some high level criteria you should review before completing a post:

  • Does it welcome new people to Perl?
  • Does it welcome people back to Perl?
  • Does it lift, encourage and praise them?
  • Is it respectful to people who's use case, experiences and journey with Perl is different to yours?
  • Is it respectful of peoples lifestyle, politics, background, and differing lived experiences?
  • Does it grow the Perl community and celebrate our diversity?
  • Does it teach people something new about Perl?

The answer in all cases should be yes.

Here are some examples that aren't inspired by any specific post but are based on content or comments I have read.


Using system perl is stupid, it's not real perl

Perl Weekly Challenge 163: Sum Bitwise Operator and Summations

These are some answers to the Week 163 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on May 8, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Sum Bitwise Operator

You are given list positive numbers, @n.

Write script to calculate the sum of bitwise & operator for all unique pairs.

Example 1:

Input: @n = (1, 2, 3)
Output: 3

Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 =>  3.

Example 2:

Monthly Report - February

Back to school ...

Apology for the delay in monthly report, usually I get it released on first day of the month.

Last month was the shortest month of the year as you all know, so getting things done became relatively harder.

What is in store this time?

Well, there were two things that took away all the attentions.

500th edition of Perl Weekly newsletter

I have been a regular reader of the weekly newsletter for many years now. If you want regular dose of Perl news directly in your inbox every Monday then please do subscribe the newsletter. I joined the elite group of editors in May 2018, thanks to Gabor Szabo for giving me the opportunity. I had the honour to edit the 500th edition of the Perl Weekly newsletter. It feels nice and gives me sense of achievments.

Perl is dead ... when I'm dead!

Too fanboy/girl-ish, perhaps?

Yes, I'm well on-trend, by a couple of months. As you see, lockdown has made a hot mess of my blogging schedule. I count myself very fortunate that is the worst effect it's had on me, alongside the gaining of some mass.

WfH WARNING! Watch out for those caramel waffles! A single Stroopwaffle has enough calories to feed a hungry village for a day and are not a sustainable treatment for anxiety. Two kWh per packet, not a word of a lie.

Perl weekly challenge 102

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Rare Numbers

You are given a positive integer $N. Write a script to generate all Rare numbers of size $N if exists. Please checkout the page for more information about it.

Examples:

  • 2 digits: 65
  • 6 digits: 621,770
  • 9 digits: 281,089,082

The solution

There is a very naive solution to this problem.

Perl Weekly Challenge 162: ISBN-13 and Wheatstone-Playfair

These are some answers to the Week 162 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on May 1st, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: ISBN-13

Write a script to generate the check digit of given ISBN-13 code. Please refer wikipedia for more information.

Example

ISBN-13 check digit for '978-0-306-40615-7' is 7.

This how Wikipedia describes the calculation of the ISBN-13 check digit:

Gzip::Zopfli - another compression module

Following on from the Gzip::Libdeflate I mentioned before, I also made this: Gzip::Zopfli

It is based on the Zopfli gzip compression library from Google Research.

Both Zopfli and libdeflate seem to excel at compressing JSON files compared to the ordinary zlib compression, sometimes with an improvement of up to 30%. Zopfli usually wins by a small margin over libdeflate. Here are some results of this script on random JSON files:

rt.cpan.org to remain online

Despite rt.cpan.org still displaying the sunset message, it is in fact not going away forever on the 1st of March, but will have an 'extended downtime' while it is moved elsewhere. In future it'd be nice if communications of such things, and even allowing others to have a say on the matter, could be handled better.

See also:

Inline::F2003

Hi all,

I started the Inline::F2003 project in 2017 because I have a strong interest in modern Fortran and Perl programming.

The project features the Perl module Inline::F2003. This module allows modern Fortran source to be inlined and called from a Perl program. The module compiles the Fortran source and builds an executable shared library that is loaded into the Perl system.

Inline::F2003 is usually invoked at compile-time. The source fragment below shows typical use of the module.

Perl Weekly Challenge 161: Abecedarian Words and Pangrams

These are some answers to the Week 161 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on April 24, 2022 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Abecedarian Words

An abecedarian word is a word whose letters are arranged in alphabetical order. For example, “knotty” is an abecedarian word, but “knots” is not. Output or return a list of all abecedarian words in the dictionary, sorted in decreasing order of length.

Optionally, using only abecedarian words, leave a short comment in your code to make your reviewer smile.

Abecedarian Words in Raku

Perl weekly challenge 101

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Task 1: Pack a Spiral

You are given an array @A of items (integers say, but they can be anything).

Your task is to pack that array into an MxN matrix spirally counterclockwise, as tightly as possible.

‘Tightly’ means the absolute value |M-N| of the difference has to be as small as possible.

New compression module Gzip::Libdeflate

I've turned the libdeflate compression library into a CPAN module:

Gzip::Libdeflate

This is the gzip compression method, but updated.

It's supposed to be much faster and better than libz (the original gzip library).

Sometimes I am getting compression of as much as 30% better on some files.

So far I haven't tested whether or not it is faster.

See the module above for links to the original library and so on.

Since this is an early release, it's very likely indeed that bugs in the module are my fault rather than any problem with libdeflate, so please report them to me.

An easy way to use WebSockets?

It's often (for work or for personal projects) that I need to create a real-time feature for a website. This can range from a simple notification whenever an event happens on the server of an existing website, to implementing a real-time multiplayer game or adding a feature inspired from social media websites.

Unfortunately the increase in complexity in code (Perl & JS) and architecture involved in setting up a reliable solution, very often made me forego the opportunity to use WebSockets for many of these projects, and instead resorted to http polling to keep the solution simple for the others to maintain.

So I started looking for a library with a client & server component, that would make life easier. The library would hopefully have to have the following features:

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.