Do-It-Yourself warnings categories

One of the reasons I have not "moved on" from Perl to some other more "modern" language is that Perl gives me such great access to its inner workings. The Do-It-Yourself Lexical Pragmas post from a couple weeks ago is an example of this. Another example is that Perl lets you tie your own code into its warnings system.

Tying into the warnings machinery requires a module. That is, the interface assumes you are reporting problems relative to another name space that invoked your code. Your module can either add diagnostics to existing Perl warning categories or actually create new categories. In either case your diagnostics are sensitive to the enablement or disablement of the category, as well as its fatalization.

In addition to enabling or disabling warning categories, use warnings ... and no warnings ...; make some subroutines available which can be used to issue your own diagnostics. These are reported relative to the file and line that called into your module (sort of like carp()). A useful subset of the warnings:: subroutines is:

Perl Weekly Challenge 147: Truncatable Primes and Pentagon Numbers

These are some answers to the Week 147 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 16, 2022 at 24:00). 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: Truncatable Prime

Write a script to generate first 20 left-truncatable prime numbers in base 10.

In number theory, a left-truncatable prime is a prime number which, in a given base, contains no 0, and if the leading left digit is successively removed, then all resulting numbers are primes.

Example:

9137 is one such left-truncatable prime since 9137, 137, 37 and 7 are all prime numbers.

Truncatable Prime in Raku

TWC 146: 10K Prime and CW Trees

In which we leap tall primes in a single bound, mis-take a tree, test percussion, and find the limits of a Curious Module.

Task 1: 10001st Prime Number - One-liners (expanded) in Raku and Perl.

Task 2: Curious Fraction Tree - Solutions in Raku and Perl (with 200+ tests), and another Perl solution using a CPAN module.

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.

My Favorite Warnings: closure

In the context of Perl, a closure is a piece of code that captures a specific instance of a lexical variable. A blog entry a month or so ago explores this in greater detail. If you review this blog entry, though, note that it does not cover lexical subroutines, which act more like anonymous subroutines even though they are named.

This blog entry, though, covers the closure warnings category.

This category has been around since warnings were introduced in Perl 5.6. I find four diagnostics in this category as of Perl 5.34.0; two related to lexical variables and two related to lexical subroutines. The "Variable" and "Subroutine" versions of these have similar causes, so I will not distinguish them in my discussion.

All of these diagnostics seem to arise from the fact that named, non-lexical subroutines are created at compile time, whereas lexical and anonymous subroutines are created at run time.

Perl Weekly Challenge 146: Prime Numbers and Fraction Tree

These are some answers to the Week 146 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 9, 2022 at 24:00). 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: 10001st Prime Number

Write a script to generate the 10001st prime number.

10001st Prime Number in Raku

Raku has a fast is-prime subroutine or method, using the Miller-Rabin algorithm, to test whether a integer is prime. We can just generate an infinite (lazy) list of prime numbers and look for the 10000st one.

use v6;

my @primes = grep { .is-prime }, (1, 2, 3, -> $a { $a + 2} ...Inf);
say @primes[10001 - 1];  # Subtract 1 because the array starts at 0

This script displays the following output:

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.

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.

Perl Weekly Challenge 145: Palindromes

These are some answers to the Week 145 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 2, 2022 at 24:00). 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: Dot Product

This first task of this week’s challenge was covered in this blog post.

Task 2: Palindromic Tree

You are given a string $s.

Write a script to create a Palindromic Tree for the given string.

I found this blog explaining Palindromic Tree in detail.

Example 1:

Input: $s = 'redivider'
Output: r redivider e edivide d divid i ivi v

Example 2:

Input: $s = 'deific'
Output: d e i ifi f c

Example 3:

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.

Developing A Game Engine with Perl: Part 6 - A Colourful Telnet Server

I'll stop reminding you that... 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

What is ANSI Game Engine?

Well, at it's core, ANSI Game Engine is a very colourful and interactive telnet server.

Why telnet!?

Perl Weekly Challenge 145: Dot Product

These are some answers to the Week 145 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 2, 2022 at 24:00). 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: Dot Product

You are given 2 arrays of same size, @a and @b.

Write a script to implement Dot Product.

Example:

@a = (1, 2, 3);
@b = (4, 5, 6);

$dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32

An important point is that we are told that the arrays have the same size. Thus, we don’t need to check that. We’ll also assume that they contain only numbers.

Dot Product in Raku

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.

Addressing CPAN vulnerabilities related to checksums

This blog post addresses checksum and signature verification vulnerabilities affecting CPAN, the cpan client, and the cpanm client, which were published in a security advisory on 23rd November 2021. If you're not aware of this topic, you might like to start by reading the advisory. This post gives a high-level description of the issues, what has been done to address them, what is still left to do, and what you should do. If you have any questions on this, you can add comments here, or email the PAUSE admins (modules at perl dot org).

Before we dig into the details, we'll first give an overview of how the relevant parts of the CPAN ecosystem work.

If you're not interested in the details, skip to the section "What do you need to do?"

TL;DR: make sure your CPAN client uses https and a trusted mirror – such as cpan.org

The Weekly Challenge - 1000 days

https://theweeklychallenge.org/blog/1000-days/

Perl Weekly Challenge 144: Semiprimes and Ulam Sequence

These are some answers to the Week 144 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 26, 2021 at 24:00). 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: Semiprimes

Write a script to generate all Semiprime number <= 100.

For more information about Semiprime, please checkout the Wikipedia page.

In mathematics, a semiprime is a natural number that is the product of exactly two prime numbers. The two primes in the product may equal each other, so the semiprimes include the squares of prime numbers.

Example:

10 is Semiprime as 10 = 2 x 5
15 is Semiprime as 15 = 3 x 5

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.

Developing A Game Engine with Perl: Part 5 - 32bit -> 64bit & Perl's Storable

If you haven't heard already... I DO NOT KNOW WHAT I AM DOING.

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.