Perl Weekly Challenge 197: Move Zero and Wiggle Sort

These are some answers to the Week 197 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 January 1, 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: Move Zero

You are given a list of integers, @list.

Write a script to move all zero, if exists, to the end while maintaining the relative order of non-zero elements.

Example 1

Input:  @list = (1, 0, 3, 0, 0, 5)
Output: (1, 3, 5, 0, 0, 0)

Example 2

Input: @list = (1, 6, 4)
Output: (1, 6, 4)

Example 3

Input: @list = (0, 1, 0, 2, 0
Output: (1, 2, 0, 0, 0)

Do-It-Yourself Lexical Pragmas

The phrase "Lexical Pragmas" is probably both redundant and ungrammatical (the correct plural of "pragma" being "pragmata", I believe). But the use of "pragma" to mean "Perl module with an all-lower-case name" is fairly common, and I wanted to make clear that this was not what I was talking about. This blog entry is about writing Perl code whose configuration changes are limited to a lexical scope, just like built-in pragmata such as strict or warnings.

There are two pieces to the puzzle: storing the configuration (at compile time) and reading the configuration.

My new modules in 2021

Perl

I had a reasonably productive year, releasing several modules that I think/hope are useful for the wider ecosystem.

Crypt::Passphrase

This module manages the passwords in a cryptographically agile manner. That means that it can not only verify passwords using different ciphers, but it also aids in gradually upgrading passwords hashed with an outdated cipher (or outdated settings) to the current one; for example when you want to upgrade from bcrypt to argon2. Password hashing is both a rather common form of cryptography, and one that is more subject to change than others; you should probably reevaluate your password handling every couple of years. With this module, you can initiate such a transition with a simple configuration change.

This also includes a number of extension distributions (e.g. Crypt::Passphrase::Argon2, Crypt::Passphrase::Bcrypt, etc…), and one new backend module (Crypt::Bcrypt)

Thread::Csp

Draft: Dancer2 Deprecation Policy

Hello fellow Dancers!

I have posted a draft deprecation policy for the Dancer2 code base for public review.

The Dancer Core Team has done our best to look at this every which way and cover all potential issues and use cases, but we're sure to have missed something here or there. So take a look and let us know. Your feedback is welcome - please add comments, feedback, and suggestions on the issue.

Thanks in advance! :-)

Perl Weekly Challenge 196: Pattern 132 and Range List

These are some answers to the Week 196 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 December 25, 2022 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: Pattern 132

You are given a list of integers, @list.

Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.

Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].

Example 1

Input:  @list = (3, 1, 4, 2)
Output: (1, 4, 2) respect the Pattern 132.

Example 2

Input: @list = (1, 2, 3, 4)
Output: () since no susbsequence can be found.

Example 3

Monthly Report - November

Time to relax before ...

What a shame, I delayed the Monthy Report once again. Although I did manage to publish last month on time i.e. 4th Nov.

Do I have reason to justify the delay?

Well, December, is always the busiest month. It is the month when I spend more time with family than ususal. Also I was busy with the Advent Calendar 2021.

As you must have guessed, today I am done with the final gift. Hence, the monthly report is getting time. There was another reason for the delay as I started Live Coding again. I have done few, which I will talk about in the end of year report in a week time, insha-ALLAH.

So, what was the highlight of last month?

My Favorite Warnings: ambiguous

... computer language design is just like a stroll in the park. Jurassic Park, that is. -- Larry Wall

Perl's grammar is inherently ambiguous. That is, it is possible for a syntactically correct chunk of Perl to have more than one valid interpretation. Maybe this is because Larry Wall is a linguist? After all, natural languages are full of ambiguity.

The ambiguous warning is part of the group syntax; that is to say, use warning 'syntax'; enables ambiguous, as well as other warnings in that group. Of course, if appropriate you can just use warning 'ambiguous'; if more precision is justified. Both warnings go back to Perl 5.6, when the warnings pragma itself was introduced.

As has become habitual, I present a random selection of instances of this warning. Normal text is from perldiag. Italicized text is mine.

Writing a SNES assembler compiler/disassembler - Day 3

Assembling the Assembler

Grammar fix

When starting implementing the compiler part of this. I noticed that the grammar does not actually really work, especially if you introduce new lines. If I parse a file with 3 instructions, we catch the \n sometime and the asm-comment token is too greedy.

Let's change the ws rule to only capture horizontal blank (space and tab) and introduce an eol token, this makes the grammar more clear on what we are working on also.

Perl Weekly Challenge 195: Special Integers and Most Frequent Even

These are some answers to the Week 195 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 December 18, 2022 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: Special Integers

You are given a positive integer, $n > 0.

Write a script to print the count of all special integers between 1 and $n.

An integer is special when all of its digits are unique.

Example 1:

Input: $n = 15
Output: 14 as except 11 all other integers between 1 and 15 are spcial.

Example 2:

Input: $n = 35
Output: 32 as except 11, 22, 33 all others are special.

Special Integers in Raku

PDL 2.063_01 released

There have been a couple of developments in PDL since the last announcement on here I could find, from 2013. To hypersummarise: 64-bit indexing, native complex number support, automatic pthreading using all available CPU cores, faster installation thanks to parallel-building, memory-mapped data, repository hosted on GitHub, easy to use "with" Inline. Returning you to the announcement:

PDL 2.063_01 has just been released. Notable changes since 2.062:

  • Various API changes (see below)
  • Improvements to $MACRONAME() handling including that arguments can now contain (balanced) brackets
  • redodims no longer leaks memory
  • The PROJ.4 interface has been updated to use the PROJ v6+ interface, finally
  • A zeroes() regression from 2.057_01 where it ignored the type of an input PDL has been fixed
  • PDL::Compression is now thread-safe (thanks Derek for the report!)

An attempt has been made to fix the older-clang/LLVM compilation errors - if you have clang 12.0.0 or earlier and can try installing this version and report results here, that would be very helpful!

My Favorite Warnings: syntax

Warnings category syntax contains a number of sub-categories representing possibly-problematic syntax. These include ambiguous syntax, problematic bareword usage, invalid printf conversions, and more. But there are also syntax diagnostics that do not fall under any of the sub-categories. These tend to be a miscellaneous group, and a normal-sized blog post can do no more than to give a sample.

What brought this to my attention was a noisy test in Template-Toolkit. under Perl 5.35.2 and up. The noisy code was untainting a variable using code like

$foo = each %{ { $foo => undef } } if ${^TAINT};

This makes use of the fact that hash keys are (so far) never tainted. The new warning was each on anonymous hash will always start from the beginning.

A (not so) simple matter of privacy

You may have seen Ovid's recent post on his discussions with the Perl Steering Committee about moving forward with implementing an initial subset of the Corinna proposal in the Perl core.

One of the issues that came up during those discussions was the best way to provide private methods in Corinna. The current Corinna proposal is that this would be done (like almost everything else in Corinna) via an attribute:

method do_internal :private () {...}

Thereafter, the do_internal() method can only be called from within the current class, and is never overridden by derived-class methods when it is called within its original class.

In other words, the :private method effectively prepends the following code to the start of the method:

croak "Can't call method 'do_internal'"
    if caller ne __CLASS__;

Perl Weekly Challenge 194: Digital Clock and Frequency Equalizer

These are some answers to the Week 194 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 December 11, 2022 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: Digital Clock

You are given time in the format hh:mm with one missing digit.

Write a script to find the highest digit between 0-9 that makes it valid time.

Example 1

Input: $time = '?5:00'
Output: 1

Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place.

Example 2

Input: $time = '?3:00'
Output: 2

Example 3

JavaScript Supported Web Scraping using Perl and Selenium

Perl Club starts to translate Japanese Perl Tutorial to English. Yuki Kimoto is one of the Perl Messengers.

Perl Club decides to write all articles English at first.

This is a first English-first article.

If you want to scrape web contents, this article explains how to scrape web contents using Perl and Selenium.

JavaScript Supported Web Scraping using Perl and Selenium


I explain JavaScript supported web scraping using Perl and Selenium::Remote::Driver. Selenium::Remote::Driver is a Perl module for Selenium. Selenium provides the APIs for JavaScript supported web scraping.

Developing A Game Engine with Perl: Part 7 - Fork

Pssssst... I DO NOT KNOW WHAT I AM DOING.

If you want to start reading from the beginning. Check out the first article in this series

Continuing from our last post, I talked about how ANSI Game Engine is a colourful telnet server. We left off with needing to fork the engines telnet server.

Player 2 has joined the game!

Time to level up our telnet server and make it multi-player with some knify forky.

Image description

I've added in the strftime identifier from Perl's POSIX module to help with time stamping the output. The setsid identifier is for starting a new session and group ID for each forked process. A.K.A, the child process. :sys_wait_h is for returning without wait after the child process has exited, using the WNOHANG flag when calling waitpid(). This provides non-blocking wait for all pending zombie children.

Zombie Attack!!!

Writing a SNES assembler compiler/disassembler - Day 2

First look at generating grammars

This will be very short even if that take me a lot of time to figure this part.

In my ASM65816Grammar.rakumod I manually wrote the Number and Addressing grammar but obiously for the instructions it's not really possible.

General ASM grammar

First let's focus on parsing something simple.

The basic gist of what you can write in an asm file is very short

lda $42 clc adc #3 cmp #0005:beq $4855 ; if $42 + 3 is 5 branch to $4855

You have an instruction per line, or you can have multiple instructions separated with a :, and ; are used to mark a comment.

Perl Weekly Challenge 193: Binary String and Odd String

These are some answers to the Week 193 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 December 4, 2022 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: Binary String

You are given an integer, $n > 0.

Write a script to find all possible binary numbers of size $n.

Example 1

Input: $n = 2
Output: 00, 11, 01, 10

Example 2

Input: $n = 3
Output: 000, 001, 010, 100, 111, 110, 101, 011

For this task, all we need to do is to print all the integers between 0 and (2 ** $n) -1, and to display the output in binary format.

Binary String in Raku

My Favorite Warnings: shadow

Who knows what evil lurks in the hearts of men? The Shadow knows!

OK, Perl does not literally have a warning about a 1930's pulp fiction and radio serial character. But Perl 5.28 introduced shadow as a new warning category for cases where a variable is redeclared in the same scope. Previously, such warnings were under misc.

To tickle this it is sufficient to

$ perl -Mstrict -Mwarnings -Mdiagnostics -e 'my $x; my $x;'

If your Perl is at least 5.28.0, you get the diagnostic

Geizhals Preisvergleich sponsors the German Perl/Raku Workshop 2022

In 2022, the German Perl/Raku Workshop will take place in Leipzig. We are very happy to announce that long time Perl supporter Geizhals Preisvergleich sponsor the workshop.

Writing a SNES assembler compiler/disassembler - Day 1

Writing a SNES assembler compiler/disassembler

Why ? Because I can. More seriously I have a project where I need to inject new Snes code in a running game and I want to express directly this new code in my Raku component (A webserver service). I want to have special sub that returns me Snes bytecode but that contains Snes assembler.

I tried injecting a SLANG in Raku already. Like writing my $byte-code = SNES lda $42; sta $54; rtl; But it’s rather tricky and I will probably just have a additional Slang with its own grammar in a dedicated file.

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.