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 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:

Debrief: Perl IDE Hackathon 2022

perl ide hackathon.png

I had a great time hacking on the Perl Navigator and Raku Navigator as part of the Perl IDE Hackathon 2022. Thank you to everyone who volunteered their time in person or remotely. Thanks especially to Brian for having many github issues ready for people to work on, and for helping so many people understand the concepts of Language Servers. I received compliments that the Hackathon was very organized but truthfully if people got that impression then Brian should get all the credit!

As a community I feel we could do better at helping people getting started and involved, so my goal was to emphasize first time and one off contributions. Brian caught the vision on this and as mentioned, did a great job preparing github issues and spent much of his time getting peoples development environment running. Hopefully he will post a report on what got done in the near future.

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!

Announcing perlcritic Policy ValuesAndExpressions::ProhibitFiletest_rwxRWX

Since several places in the Perl documentation caution against the use of the file access operators (-r and friends), and since I was unable to find a Perl::Critic policy dealing with this, I thought I would make one: Perl::Critic::Policy::ValuesAndExpressions::ProhibitFiletest_rwxRWX.

This policy is assigned to the 'bugs' theme. It has low severity because there are some uses of these operators that seem legitimate to me -- or at least I see no easy way to get around their use.

On the one hand, something like

-r $file or die "File $file not readable\n";
open my $handle, '<', $file;

is wrong several ways. On the other hand, it is hard to see how to implement File::Which without the use of -x. And in fact it does use -x.

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

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.

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.

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

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

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 ];

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

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.

Strawberry Perl

Do you want to convert Perl source into .exe?

Please checkout my experience.

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

Perl Weekly Challenge 217: Sorted Matrix and Max Number

These are some answers to the Week 217 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 21, 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.

Task 1: Sorted Matrix

You are given a n x n matrix where n >= 2.

Write a script to find 3rd smallest element in the sorted matrix.

Example 1

Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3])
Output: 1

The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5.
The 3rd smallest of the sorted list is 1.

Example 2

Annotated Perl::Critic Policy Index

In the wake of my postings on the file access tests (-r and friends) I wondered if there was a Perl::Critic policy to find them. So I constructed an annotated index of Perl Critic policies. Because of its size I stuck it on GitHub rather than in-line to this blog post.

This index assumes that any CPAN module whose name begins with Perl::Critic::Policy:: is a Perl Critic Policy. The index entry for each module contains the name of the module itself (linked to Meta::CPAN), the name of the distribution which contains it, and the abstract for the module if it contains anything other than a repeat of the module name. I suppose the module description could have been added, but I hoped the abstract would be sufficient.

This operation gave me 341 policies. I did not find the policy I wanted among them. In fact, only Kevin Ryde's Perl::Critic::Policy::ValuesAndExpressions::ProhibitFiletest_f came close.

For those who want context, the relevant blog posts are:

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).

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.