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.

Climbing the Charts (request for feature requests)

Hurray, released another version of Chart without new features. Actually rewrote the complete documentation and I guess especially this page (with a little help of this list) is all what most people need. That also allowed us to drop the old PDF and HTML docs which took 8/9 of the distributions space (good side effect).

But main reason to tell this: writing the docs forced me to plough through most of code and also test most of it with self written or at least adapted code, which brought me into the position actually understand what I maintain.

So now I can give actually intelligent responses to feature requests. So please post them here or there. And yes , there will be SVG support, but not soon.

Sorting Subroutine Results

The Perl sort built-in is mostly (at least by me) called as sort LIST or sort BLOCK LIST. But there is a third way to call it: sort SUBROUTINE LIST, which actually appears first in the documentation.

This is not a blog entry about using the sort SUBROUTINE LIST form of sort. It is more about the need to be aware of this form when writing (or trying to write) the sort LIST form.

Consider the following situation: you have a subroutine foo() which returns an un-ordered list. You need that list sorted. Perl has a sort built-in, so your (or at least my) first reaction is to write my @sorted = sort foo();, run it, and then wonder why @sorted is empty.

Ideas from TPRC2022: Bug/Task/Issue independent interface and a cli tool

We have a Database Independent Interface aka DBI and a Unified Cache Handling Interface aka CHI which both provide a generalized interface to similar backend services. Similarly we have AnyEvent - the DBI of event loop programming and Log::Any. With the Nopaste cli provides an agnostic tool to send data to pastebin like services.

So why not a generalized interface to Bug, Task, and Issue trackers? And an accompanying cli tool?

Jira is obviously very common for organizations to use, Github issues are ubiquitous, Bugzilla is still around, Request Tracker has a strong following, not to mention Zendesk, OTRS, and so many others you're forced to use each day for your work. Likely your organization or employer uses more than one!

Having a generalized interface makes automation interactions more trivial and vastly more consistent. With this interface established, an accompanying command line tool becomes a logical addition. And if you aren't yet automating interactions with your Bug / Task / Issue tracker, then having an interface is even more important to getting you started.

Such an interface would be the majority of whats needed for a unified interface and cli tool and to interact with CPAN modules bugs/issues regardless if they are on Github, RT, or other. Something similar to Debian's reportbug command.

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:

TOTP with Perl and Authen::OATH

I wrote this post after seeing Flavio Poletti's blog post entitled OATH Toolkit. I have been a fan of time-based one time passwords (TOTP) for many years. In fact, I used Mobile-OTP in commercial applications for several years before the Initiative for Open Authentication (OATH) and OATH/TOTP were codified in RFC6238.

Now-a-days, OATH/TOTP is the best choice for time-based one time passwords, and has been for at least a decade.

Introduction

I have implemented OATH/TOTP many times in my career, and in several programming languages, including Perl. One of those Perl implementations is within the open source program kpcli.

Using Authen::OATH for TOTP

Using Authen::OATH for TOTP is straightforward. The kpcli code appears a little overly complex because it optionally demand-loads the Authen::OATH module and has support for digest ciphers other than SHA1. This is kpcli's get_totp() subroutine:

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.

ERROR: 'flock' trapped by operation mask at /usr/lib64/perl5/vendor_perl/Storable.pm

Hello All,
Facing flock error when trying to execute the Automated unit testing .

705:041754.467 CPM0 frl-plugin:perlscript: ERROR: 'flock' trapped by operation mask at /usr/lib64/perl5/vendor_perl/Storable.pm line 268.
Compilation failed in require at /usr/share/perl5/vendor_perl/Const/Fast.pm line 15.
Compilation failed in require.
BEGIN failed--compilation aborted.

Could anyone please help on the issue

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

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!

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!

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

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.

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.

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

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

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.

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.