Perl Weekly Challenge 254: Three Power

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

Task 1: Three Power

You are given a positive integer, $n.

Write a script to return true if the given integer is a power of three otherwise return false.

Example 1

Input: $n = 27
Output: true

27 = 3 ^ 3

Example 2

This week in PSC (120) | 2023-10-12

This week we discussed the following topics:

  • load_module has a PR in progress; needs continued nudging on a few last issues so we can move forward and merge it
  • Pod “U<..>” for underline has better docs; again that could possibly be merged now if all are happy
  • We spent further time thinking about other ideas for extending and improving Pod: further details may be published in due course
  • We briefly discussed how to break the logjam on PPC0013 and continue work on join/etc…

Why we use Perl at our company

I've been programming in Perl for almost two decades now. A lot has changed in Perl, and also in the way I program with it.

Several years ago my partners and I started a software company, I was in charge of everything to do with tech, so as CTO it was my responsibility to choose the tech stack our company was going to use. And it was an easy one: Perl.

Years have gone by, our company has continued to grow, our product catalogue has increased and the amount of data processing and analysis we do it's exponentially higher than when we started. And Perl has continued to show what a great option it is.

I would like to list some of the reasons why Perl has been such a great fit for our company.

Versatility

Perl v5.36, Debian, and Ubuntu

I normally only write about the Perl Weekly Challenge/The Weekly Challenge, but today I have a couple things to say about recent Perl versions. I've been watching the videos from TPRC2023, and loving the talk by Paul Evans What's New in Perl v5.38. I like to have a minimum version in my code generally that I can rely on having all the features from. I think I previously ignored v5.36 because it wasn't in Debian yet (and I typically only use packaged Perl), but thanks to Paul's talk I've really enjoyed looking at the perldelta, where some really groundbreaking Modern Perl changes were made just in the use v5.36 syntax.

Well, now two things have happened. v5.36 is the oldest release still officially supported by the Perl team, and it's in Debian stable. This makes it a great candidate to be my new target version for most code!

Unfortunately, Ubuntu 22.04 (one of my servers) is still stuck on v5.34, and this makes me sad. 😥

In related news, the new object system in v5.38 and beyond looks pretty sweet! I'm excited. Can't wait to see what else gets implemented in the next couple versions.

Perl Weekly Challenge 253: Weakest Row and Schwartzian Transform

These are some answers to the Week 253, 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 January 28, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Weakest Row

You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0.

A row i is weaker than a row j if one of the following is true:

a) The number of 1s in row i is less than the number of 1s in row j.

b) Both rows have the same number of 1 and i < j.

Write a script to return the order of rows from weakest to strongest.

Example 1

Matching simply

A little over ten years ago, when Perl 5.18 was approaching its release date, I released match::simple. This was mostly in response to the smartmatch operator (~~) being deprecated, but also a solution to the incredibly arcane rules for how the smartmatch operator operated.

match::simple was intended to be... simpler. The operator looks a little funky because it uses some clever trickery to fake being an infix operator:

    use match::simple;
    
    if ( $this |M| $that ) {
        ...;
    }

When Saif Met Sarah

The Perl and Raku Conference 2023

The Perl and raku Conference 2023 in Toronto was an event I was really looking forward to. A chance to rub shoulders with the giants of Perl and Raku, absorb insights and innovations of the nerdy Perly community...everything they said in the blurb. It was very well organised thanks to Alex, Sawyer, Makoto, Amber and Peter, herding an army of volunteers. Such a knowledge-intensive event should be recorded for posterity, trapping the wisdoms and inspirations from the wise and inspiring in the container that is YouTube.

TPRC Toronto Part 3 The End Game

Well a nice banquate last night with good food and good company but a little sad that it was the last day.

To start off I atennded Sawyer X rather sad talk 'No one is Immune to Abuse' I cannot give this talk do justice in a short blog post so I suggest you seek out the recorded talk when it comes out. I will see if I can repost it here once it comes out. It had a large number of lessons for all of us in the Perl comunity.

Next up was me but unfortunetly I have some connection errors and had a little shorter talk and but both of the attendees gave me good feedback ;) oh well that is what happens when you are scheduled at the same time by two of the 'Big Wigs' in the Perl community. Luck of the draw I guess.

Perl Weekly Challenge 253: Split Strings

These are some answers to the Week 253, 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 January 28, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Split Strings

You are given an array of strings and a character separator.

Write a script to return all words separated by the given character excluding empty string.

Example 1

Input: @words = ("one.two.three","four.five","six")
       $separator = "."
Output: "one","two","three","four","five","six"

Example 2

Input: @words = ("$perl$$", "$$raku$")
       $separator = "$"
Output: "perl","raku"

Split Strings in Raku

Perl Weekly Challenge #225 - Words to the Max and Diff Sum

Hi all! Back this week with both solutions to The Weekly Challenge for once. We've got a word counting challenge and one that I really don't know how to explain. You have to see the challenge to understand it.

Max Words

So this challenge is just to tell us what the longest sentence in a set of sentences is. How many words does it have? A very simple easy solution can follow:

my $highest;
foreach (@ARGV) {
    my @words = split(/ /, $_);
    shift @words if !$words[0];
    $highest = scalar @words if scalar @words > $highest;
}
say $highest;

Support TPRF with Perl 5.38 swag purchases

800.jpg800a.jpg

Shirts and hoodies are available in variety of colours, sizes and styles. Stickers, mugs, and phone covers are also all available. With proceeds from sales funding The Perl and Raku Foundation.

Buy from https://the-perl-store.creator-spring.com/

Faster and up to date HTTP Cookie Jar

Hot off the keyboard, is HTTP::State in trial form anyway. This is HTTP cookie jar supporting public suffix, same site, cookie partitioning and other goodies from RFC6265bis-draft and CHIPS. It makes cookie handling on the client side more in line with recent developments in browsers (browsing context, first-party partitioning etc).

While most of 'browsing context' and partitioning features are not directly usable in current Perl HTTP user agents, it eases the path for better cookie support in new/updated user agents.

In the mean time, a compatibility API allows drop in usage where ever HTTP::CookieJar is used. The main benefit for current user agents is a significant boost in cookie retrieval performance.

It is a trial of a first release, so probably has some teething issues and is currently limited v5.36.0 min perl. However if you want to give it a try, please do and give feedback if you can!

Perl Weekly Challenge 252: Unique Sum Zero

These are some answers to the Week 252, 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 January 21, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Unique Sum Zero

You are given an integer, $n.

Write a script to find an array containing $n unique integers such that they add up to zero.

Example 1

Input: $n = 5
Output: (-7, -1, 1, 3, 4)

Two other possible solutions could be as below:
(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4).

Example 2

SQL::Inserter for faster/multi-row inserting

SQL::Inserter is new CPAN module based on what we are using at SpareRoom to easily manage buffered inserts, as well as to replace SQL::Abstract's slow insert method.

Inserter OO interface

The idea is that if you want to insert many rows of data (e.g. part of an ETL pipeline, or writing logs etc), you'd want to be doing that with multi-row INSERT statements. With SQL::Inserter you create a very lightweight object to handle inserting for you, just pass it a DBI db handle, the table name and the columns you'll be inserting (optional if you use hashes for inserting, see next section):

Perl Weekly Challenge #223 - Count Primes? I've Never Met the Man

Hello everybody! It's another week with a new Perl Weekly Challenge. This week I'm only doing the first challenge, not because of time, but because the second challenge makes absolutely no sense to me. Perhaps a clarification will come out, but I'm not going to bother at the moment.

This week the challenge is to find the number of primes under the provided number. This is a challenge that really isn't worth rebuilding, and so I would recommend using Math::Prime::Util. You have to know when to just trust the professionals and use modules. With the use of M::P::U, we can essentially do the challenge in one line.

use Math::Prime::Util 'primes';
say scalar @{primes(shift)};

primes() returns an array reference, so we have to dereference the array after calling the function. We shift the number that is entered, call primes (which provides an array of the primes under that number), and dereference it and count it to print the answer.

It's that simple! This is a case where I definitely wouldn't recommend writing your own custom prime finder. I'll hopefully see you next week with the next challenge!

Celebrate Perl 5.38 with Limited Edition merch!

2.png

To celebrate the upcoming release of Perl 5.38 we are excited to offer Limited Edition* merchandise.

The design was a true team effort by the marketing committee and is inspired by the traditional Perl camel logo. We had a lot of fun throwing around ideas, then throwing them in to AI to see what it's randomness would come back with. We then passed the ideas to our artist for the final result.

Check them out on the official Perl store where proceeds go to The Perl and Raku Foundation which then directly fund grants etc.

* TBD how limited.

Perl Weekly Challenge 252: Special Numbers

These are some answers to the Week 252, 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 January 21, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Special Numbers

You are given an array of integers, @ints.

Write a script to find the sum of the squares of all special elements of the given array.

An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0, where n is the length of the given array. Also the array is 1-indexed for the task.

Example 1

This week in PSC (119) | 2023-10-05

This week we discussed the following topics:

  • Module::CoreList is still somewhat awkward and manual to update around releases. We need better instructions and possibly some automation around the simpler
  • Pod’s new U<> format needs more documentation around its limited availability.
  • Test2::Suite sometimes spuriously fails in perl core; needs some poking
  • We should put out a call for bugfixes for a possible 5.38.1 release

Perl Weekly Challenge #222 - Checking Against My List of Members

Hi everybody! Just doing the first weekly challenge task again this week. This week we're sorting a list of numbers and then checking whether the number matches the same position in the unsorted list. It's a very simple challenge and easily written in about 4 actual lines of clean code.

Here's the code:

#!/usr/bin/perl
use strict;
use v5.24;
my @sorted = sort @ARGV;
my $matches;
for (my $i = 0; $i <= $#ARGV; $i++) {$matches++ if $ARGV[$i] == $sorted[$i]}
say $matches // 0;

Profiling Perl under Apache

At SpareRoom we still use Apache/mod_perl and, despite Devel::NYTProf::Apache's warnings about it not being maintained, it is still the best way for profiling Perl.

All you need to do really, for a mod_perl app, is to include this in your Apache config:

PerlPassEnv NYTPROF # Not needed for default settings.
PerlModule Devel::NYTProf::Apache

You should also allow a single worker/child, and make sure it terminates cleanly before processing the output (nytprofhtml).

Since I want my dev/test environments to normally run with multiple workers and no profiler attached, I have a bash alias that exits apache and restarts it set-up for profiling. I thought I'd share an example of that, as I find it very useful.

First, a basic example pulled from what I used on our older CentOS/RedHat VM:

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.