Ideas from TPRC2022: Tools to help refactor large mature code bases

Every Perl gig I have ever had, and from most of the conversations I had at this years Perl and Raku Conference, was working on a large code based that is serving the business and it's customers very well such that the business is profitable (i.e. a mature code base).

This is an enviable position to be in but whilst this software is robust from the outside, there is often a reluctance to make dramatic changes. Unfortunately code that is perceived as too fragile to touch tends to be replaced and replaced in another language.

The PPI + Class::Inspector combination is already being used by people I've spoken too, in bespoke tools to refactor large code bases reliably.

We have frameworks for testing (Test:: etc) and linting (Perl::Critic) with suites of well established reusable polices, so why not a similar framework for refactoring?

Such a tool might have provided polices to assist replacing deprecated syntax, migrate between "Try" implementations, fixing up scalar vs list context, just to name a few.

Scalar Context: Lists Versus Arrays

For a long time after I first encountered Perl, I looked on "list" and "array" as essentially interchangeable concepts. A list was simply the source construct corresponding to an array. This idea is mostly correct. But as they say, the devil is in the details.

One of the differences is what happens to them in scalar context. An array evaluates to the number of elements it contains. A list evaluates to its last element. So:

my @array = qw{ one two five };
say scalar @array;  # prints '3'
{
    no warnings 'void'; # Note the need for this
    say scalar( qw{ one two five } ); # prints 'five'
}

Perl Weekly Challenge 221: Good Strings

These are some answers to the Week 221, task 1, 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 June 18, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Good Strings

You are given a list of @words and a string $chars.

A string is good if it can be formed by characters from $chars, each character can be used only once.

Write a script to return the sum of lengths of all good strings in words.

Example 1

Input: @words = ("cat", "bt", "hat", "tree")
       $chars = "atach"
Output: 6` 

The good strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.

German Perl/Raku Workshop recordings are online

During the last days, we reviewed and cut the video recordings. The recordings
are now available on the media platform of the CCC:

https://media.ccc.de/c/gpw2022

Some of the presentations are not yet published - we need to work on
the video some more..

Again, thanks to our speakers, our sponsors and everybody else for the
great conference. Next year we'll hopefully meet again in 2023 in Frankfurt am Main in person!

Mite: an OO compiler for Perl

Moose is great, but it does introduce a slight performance hit to your code. In the more than 15 years since it was first released, hardware improvements have made this less of a problem than it once was. Even so, if performance is a concern for your project, Moose might not be what you want. It also has a fairly big collection of non-core dependencies.

Moo is a lighter weight version, minus with meta-object protocol, but supporting nearly all of Moose's other features. It loads faster, sometimes runs faster, and has fewer dependencies. (And most of the dependencies it does have are just modules which used to be part of Moo but were split out into separate distributions.)

But what if you could have fast Moose-like object-oriented code without the dependencies?

In 2013, Michael Schwern started work on Mite to do just that. It was abandoned in 2014, but I've taken it over and expanded the feature set to roughly equivalent to Moo.

Mite is an object-oriented programming compiler for Perl. It allows you to write familiar Moose-like object-oriented code, then compile that into plain Perl with zero non-core dependencies. Your compiled code does not even have a dependency on Mite itself!

We have a new Perl Steering Council for 2022/23

Following the release of 5.36.0, we have a new Perl Steering Council (PSC). The PSC for the next year comprises Ricardo Signes (RJBS), Paul Evans (PEVANS), and Philippe Bruhat (BOOK). These three will serve until 5.38.0 is released, at which point the next PSC will be elected.

Perl Weekly Challenge 220: Squareful Arrays

These are some answers to the Week 220, task 2, 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 June 11, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Squareful Arrays

You are given an array of integers, @ints.

An array is squareful if the sum of every pair of adjacent elements is a perfect square.

Write a script to find all the permutations of the given array that are squareful.

Example 1:

Input: @ints = (1, 17, 8)
Output: (1, 8, 17), (17, 8, 1)

(1, 8, 17) since 1 + 8 => 9, a perfect square and also 8 + 17 => 25 is perfect square too.
(17, 8, 1) since 17 + 8 => 25, a perfect square and also 8 + 1 => 9 is perfect square too.

Example 2:

Smart Match in CPAN

There is nothing like looking, if you want to find something. -- The Hobbit, iv, "Over Hill and Under Hill"

Recently on the p5p mailing list the topic of removing smart match re-surfaced. There was a fairly vigorous discussion about the effect this would have on CPAN. So I thought I would look into how many uses there actually were.

Fortunately there are Perl Critic policies for this: Jan Holčapek's Perl::Critic::Policy::ControlStructures::ProhibitSwitchStatements and Perl::Critic::Policy::Operators::ProhibitSmartmatch. All I had to do was run them against my mini-CPAN.

My results:

  • Total distributions: 40704
  • Distributions with violations: 842
  • Files with violations: 1568

A look at the file names involved says that about two-thirds of the violations are in the published modules themselves, and the rest are in support code (directories t/, inc/, and the like).

It is possible that the results of Perl::Critic::Policy::ControlStructures::ProhibitSwitchStatements contain false positives simply because someone implemented subroutines named given() or when() unrelated to smart matching.

Trying to contact Francis van Dun (FVANDUN)

I am trying to contact Francis van Dun (FVANDUN on cpan) for permission to relicense Net::DHCP to the MIT license.

The email listed on cpan bounces. If you are out there Francis please give me your blessing via the above link.

Decode Hexdump

Please checkout my experience decoding hexdump.

https://theweeklychallenge.org/blog/decode-hexdump

Perl Weekly Challenge 220: Common Characters

These are some answers to the Week 220, task 1, 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 June 11, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Common Characters

You are given a list of words.

Write a script to return the list of common characters (sorted alphabetically) found in every word of the given list.

Example 1

Input: @words = ("Perl", "Rust", "Raku")
Output: ("r")

Example 2

Input: @words = ("love", "live", "leave")
Output: ("e", "l", "v")

Common Characters in Raku

My Favorite Modules: File::stat

File::stat overrides the core stat() and lstat() functions. Instead of arrays, the new functions return an object having methods corresponding to the elements of the arrays returned by the original functions. This module has been in core since Perl 5.004.

The advantage of this module is clearer code. For example, to get the size of file $file without it is something like

    my $size = ( stat $file )[7];

But with this module the same effect is given by

    my $size = stat( $file )->size();

Once you have the object in hand, you cam query it for any of its properties, so if you want both size and modification time, instead of

    my ( $size, $mtime ) = ( stat $file )[ 7, 9 ];

MooseX::Extended Tutorial

There's been a lot of work on MooseX::Extended and now it comes with a fairly extensive tutorial.

The basics are pretty easy to learn, but it gives you a good amount of power. It also allows you to easily define custom versions so you can just slap use My::Custom::Moose; (or role) at the top or your code and it works just fine.

You can now disable just about any features in it you don't want. You can also include experimental features, such as multimethods (based on number of args) and async/await.

Check out the github repo if you'd like to contribute.

Perl IDE Hackathon 2022

perl ide hackathon.png

On Tuesday 21st June there will be a Perl IDE Hackathon in the Hackathon room at The Perl and Raku Conference. You can also participate in this event remotely.

Our goal is to enhance IDE and Editor support for Perl 5 which is typically via plugins, with an an emphasis on helping people make their first every contribution. It's not even required that you use the IDE/Editor that you are helping out with.

With support from their authors, I am hoping we can help make meaningful contributions to the following, :

- Jetbrains IDEA plugin
- Language Server's for VSCode etc. i.e. Perl Navigator, Perl::LanguageServer, PLS
- Vim plugin vim-perl
- Emacs
- Others!

Recall that IDE support is a priority for the community and we need your help!

If you are attending the Perl and Raku conference then simply turn up at the venue on the 21st with your Laptop and find the room! There will be wifi and we will work together with everyone in the slack channel.

Perl Weekly Challenge 219: Sorted Squares

These are some answers to the Week 219, task 1, 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 June 4, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Sorted Squares

You are given a list of numbers.

Write a script to square each number in the list and return the sorted list, increasing order.

Example 1

Input: @list = (-2, -1, 0, 3, 4)
Output: (0, 1, 4, 9, 16)

Example 2

Input: @list = (5, -4, -1, 3, 6)
Output: (1, 9, 16, 25, 36)

Sorted Squares in Raku

Strawberry Perl

Do you want to convert Perl source into .exe?

Please checkout my experience.

https://theweeklychallenge.org/blog/strawberry-perl

Core Modules: filetest

The filetest pragma modifies the behavior of the file test operators (a.k.a. the -X operators.) It has been in core since Perl 5.6.0.

As of Perl 5.36.0 it still has only one sub-pragma, 'access', which applies to the -r, -w, -x, -R, -W, and -X tests. Normally, these tests only consider the mode bits returned by stat(), as discussed in my previous blog post, The File Access Operators: To Use, or Not to Use. But within the scope of use filetest 'access';, these tests consider not only the mode bits, but any ACLs (Access Control Lists) that may be applied to the file -- at least, under POSIX systems. Under systems that do not implement access(), this sub-pragma does nothing.

Taking ACLs into account sounds like a Good Thing, but it comes at a price. Within the scope of the 'access' sub-pragma:

Entering the Charts

As part of HalleLeipzig.pm I had my duties to co-organize the recent German Perl Workshop but also the opportunity to give some talks. (Recordings are online [EDIT] now at the CCC video platform). My main talk was about plotting data with Perl (english slides).

Perl Weekly Challenge 218: Maximum Product and Matrix Score

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

Note: the programs presented here were written several days ago, but I was unable to write this blog post in time due to various reasons, including lack of time and some serious problems with my computer.

Task 1: Maximum Product

You are given a list of 3 or more integers.

Write a script to find the 3 integers whose product is the maximum and return it.

Example 1

Input: @list = (3, 1, 2)
Output: 6

1 x 2 x 3 => 6

Example 2

Memory Leak in Perl

After a very long time, I found time to blog and here is my journey to deal with memory leak in Perl.

https://theweeklychallenge.org/blog/memory-leak/

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.