Look, that's why there's rules, understand? So that you think before you break 'em. — Terry Pratchett, in Thief of Time.
A couple installments into this series of blog posts, I realized an introduction to Perl warnings would be appropriate, with some words on how to turn them off, and why you probably do not want to. Yes, this should have been the first post in the series, but I didn't know it would actually be a series, and, well, maybe better late than never.
The Perl compiler/interpreter can emit warnings when it finds things that may indicate a problem with the code. These are not (yet) enabled by default, but experience has shown that enabling them can highlight conceptual and logic errors.
Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on Aug. 14, 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: Damm Algorithm
You are given a positive number, $n.
Write a script to validate the given number against the included check digit.
Well, "solve" may be a strong word for what I did with this problem, at least for my programming conscience. The problem statement was simple, but had no requirements for an acceptable solution other than what you could infer from the example solution in the problem statement. However I did give not one, but two solutions, so that's not totally lazy, even if each, on its own, is lazy, right?
Perl has various ways to transfer control out of the normal flow: die(), goto(), return(), next, last, and redo are among the sorts of things I mean. Not all of these are suitable for all circumstances, and Perl issues an exiting warning for unsuitable cases.
Sometimes, though, you just need to do something unsuitable. When I write an interactive script, it generally dispatches to a subroutine named after the function, something like so:
Since my last post I wanted to take my EV charge calculator script and convert it into a web form. In this post I breakdown how I migrated the script to a Dancer2 web app.
Just a minor note for those readers who may not be aware, Dancer2 is a "lightweight web-framework for Perl" as described in Dancer2 documentation and can be similar in comparison to Ruby Sinatra and Python Flask.
I started by creating a new project folder with the dancer2 program ( # please note that I am working on a Windows PC and on a linux OS the program would just be named dancer2 )
dancer2.bat -a EVCalc
I then copy over the files inside this directory to my project repository as shown in this commit and moved all the scripts to the bin directory as shown in this commit
I wrote a script to covert the electric rate csv file into a module and added all the calculation code in the main project module. All the html markup is stored in this template file and I sprinkled some CSS which renders a page similar to the screenshot below after submitting some data for calculating the charge of an EV :
Thank you for your time, I hope you enjoyed my post.
I recently added some files for SQL 2003 and SQL 2016, created by Domingo Alvarez Duarte.
Specifically, look for:
o sql-2003-2.ebnf
o sql-2003-2.ebnf.readme
o sql-2003-2-railroad-diagrams.xhtml
o sql-2016.ebnf
o sql-2016.ebnf.readme
o sql-2016-railroad-diagrams.xhtml
I'd suggest downloading the *.xhtml files and viewing them locally, rather than hammering the on-line convertor mentioned in the readme files, which accepts *.ebnf files and displays these railroad diagrams.
And that begs the question: Is there any Perl code which converts a grammar into a railroad diagram?
Spoiler Alert: This weekly challenge deadline is due in a few days from now (on August 8, 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: Happy Women Day
Write a script to print the Venus Symbol, international gender symbol for women. Please feel free to use any character.
The Perl compiler wants to help us write clean code. One of the ways that it does this is to issue warnings when a global variable appears ony once: Name "main::Foo" used only once: possible typo at ...
The thing is, sometimes this is not an error. For example, we may want to refer to a global variable in another package, one that was not imported into our namespace.
I have seen various expedients used to avoid this warning in CPAN code. Something like $Foo::Bar = $Foo::Bar = 42; is fairly typical. Sometimes this strange-looking code is commented as to its purpose, others not.
Alternatively, you can use the pragma no warnings 'once'; to supress this warning. This seems to me the appropriate way to spell "I meant to do that!" under the circumstances:
In this time, I review constructor argument customize. BUILDARGS can customize contructer argument. It is good enough for me. I think existing library that receive hash references as argument will have a slight performance penalty because a BUILDARGS function call.
I'm always doing other things and then Sunday comes and I start thinking, "How much time do I have before it's midnight in London?"
When "The Perl Challenge" first started, I was happy to just ponder the problems. Then came the pandemic and I thought that I would use some of my then copious free time to contribute. Then time got not-so-copious. And more people started contributing to TWC, some people much more talented than me, it turns out.
So I'll take a stab at things when I can and I'll still try to write a stand-alone script the way (I wish) I would at work, but my threatened laxness in writing things up will be more of a promise: Light banter to cast a veneer of confidence on the correctness of my results, anything else is extra.
Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on Aug. 7, 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: Permuted Multiples
Write a script to find the smallest positive integer x such that x, 2x, 3x, 4x, 5x and 6x are permuted multiples of each other.
For example, the integers 125874 and 251748 are permuted multiples of each other as
251784 = 2 x 125874
and also both have the same digits but in different order.
It's very slow if you have more than a few thousand keys in memcached. Not an unusual use case I think? I've got a fix here, which appears to DTRT: https://rt.cpan.org/Ticket/Display.html?id=138133. Maybe? I didn't spend too long looking at memcached's low level wire protocol.
The patch passes all the module's current tests and works for us. It took our CPU load from being pegged at 75% all of the time to being idle. So, if you're using Cache::Memcached, and the ->stats method (which isn't in the XS version of the module) then you might want this patch.
On that note - who is maintaining Cache::Memcached? The last release was in 2012. This isn't a high river module, but any app of significant size or age is *probably* using it and if they're using the ->stats method then ... Sure there's the ::Fast version, but I suspect this version is in a lot of places.
So if you know someone who knows someone who can prod the current maintainers then please point them at this post/patch. If you're using Cache::Memcached then perhaps try out this patch as well.
I explain how to create your website using Giblog and Perl by live coding. Giblog is a Perl module to create web sites. If you see this movie, you can create your web site using Giblog and Perl(although you need some Linux knowledge).
Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on July 31, 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: Last Sunday
Write a script to list Last Sunday of every month in the given year.
For example, for year 2022, we should get the following:
Note: the current version of Perl in late April 2018 was 5.26.2, so all reports on more recent perls came in (long) after the initial testing which I’m interested here. The relevant part of the page is therefore the part from 5.26.2 on down.
Last time, reports started coming in within hours of the release; over 60% of the picture was there within a day; some 85% after 2 days; and the first wave of reports lasted a week.
This time, it took almost a day to even start getting reports, and the diversity has been much lower. 3 days in, reports are still absent for many platforms:
I was just going to use a variation on last week's "Nybble Swap" task for this, but then I foolishly thought, "No, I've read Hacker's Delight I should twiddle bits!"
Okay, now that I told you about my first mistake, let me tell you, the bit twiddling was fun once it started working but I do have a little regret that it's straightforward bit-twiddling: I created a bit mask to pick out alternating bits, then just did the shifting and OR-ing you would expect to switch the even/odd bits. I can't help but wonder if there isn't some clever one-liner in Hacker's Delight. I can't claim to have remembered it even once though: I skimmed the book.
Some contents of the PerlMonks article are largely repeated here.
This piece of PerlMonks article is already 19-year-old, but it is
still valid. One of the good things of the article is that you need not
install new modules or programs if you are on a *nix system.
Prerequisites
One should have some knowledge on modules, packages and, not really necessary, object-oriented Perl ("Perl OO" in short).
Originally, the Perl Weekly Challenge called for solutions in Perl and Raku (also known as Perl 6 at the time). But, very soon, people started to provide solutions in other “guest” languages. See for example my blog post providing solutions to the task described below in about 18 different guest languages.
One of the languages I tried for the first time last week with Sylvester’s sequence is dc, and it turned out to be much more difficult and challenging than I initially thought. One of the problems is that there is only very limited documentation on this old programming language. So I thought it might be useful to describe in some details how I solved it. I provided detailed explanations in this other blog post. I’ll now do the same with the disarium number task of this week, which is a bit more complicated.