The open or die idiom is fairly ubiquitous in Perl. To be useful, the exception should say something about what went wrong: open ... or die "Open error: $!", for example.
The $! built-in variable (a.k.a. $ERRNO or $OS_ERROR if use English; is in effect) gives you access to the C language errno variable, which through the magic of Perl interpolates an error message appropriate to the error given.
But there are times when some error analysis is in order. Fortunately, $! is a dualvar, so if you access it in numeric context rather than string context, you get the actual numeric value of the error code. But for both portability and maintainability you don't want to compare $! to a numeric literal. Thus, Errno. For example:
Spoiler Alert: This weekly challenge deadline is due in a few days from now (on April 16, 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: Jumping Letters
You are given a word having alphabetic characters only, and a list of positive integers of the same length.
Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.
I'm going to start this blog by writing a very simple guide on installing Perl using perlbrew.
To install Perl using perlbrew, first visit the website and grab its
curl -L https://install.perlbrew.pl | bash
and paste it in your terminal, and wait for it to do its job.
Now you will have to run
perlbrew init
to initialize perlbrew.
After that you will probably have to add it to your $PATH environment variable as the installation says after it is done downloading. This can be done with a command like
At $work, one of my colleagues who is not in a developer role has started to get into writing code more and more, and I am mentoring him. He's about to work on a productive ticket with Moose for the first time, so I gave him a little reading list, mostly involving selected parts of the documentation as well as Ricardo Signes' excellent talk Moose is Perl.
But then I thought I must have read lots of great blog posts about Moose on the Perl Advent Calendar over the years. I tried to find a few, but had some trouble identifying them easily. So I wrote a quick scraper. Here are all articles that mention Moose since 2010 (where the format of the website changed). Most of them are about Moose or one of the numerous MooseX modules.
Perl has two operators, cmp and <=>, which are basically never seen outside of sort blocks.
That doesn’t mean you can’t use them elsewhere, though. Certainly sort and these operators were designed to work seamlessly together but there isn’t anything sort-specific about the operators per se, and in some contexts they can be the most appropriate solution.
An example would be some code that used to go (almost exactly) like this:
Just in case the problem passed you by, Rene "cavac" Schickbauer has a post discussing a Zlib CVE, and the implications for cpan modules:
I have done a casual grep through my local CPAN mirror (yay for local mirrors!), which has given me a list of potentially vulnerable modules. There are over 90 of them. Yes, there are probably a few false negatives and a few false positive, as i didn't have time to go over each distribution in detail.
Please check your CPAN distributions for any use of zlib.c, libz.c, deflate.c, compress.c and similar variants and update as necessary. If at all possible, i would also recommend to switch to either the zlib provided by the operating system or at least coordinate with other CPAN authors to reduce the number of static copies of the zlib libraries spread all over CPAN modules.
Write a script to get the maximum points. You are allowed to take out (kill) any integer and remove from the list. However if you do that then all integers exactly one-less or one-more would also be removed. Find out the total of integers removed.
Example 1
Input: @int = (2, 3, 1)
Output: 6
First we delete 2 and that would also delete 1 and 3. So the maximum points we get is 6.
I've been using perlimports a lot at $work. I'm generally quite happy with perlimports, but it can get confused by modules which are being dynamically used. Fortunately, there's one little trick that can help in this scenario.
A couple days ago the SD card on a Raspberry Pi lost its beady little mind, and I ended up rebuilding the system from scratch. I generally build my own Perl (also from scratch) and then install the modules I need. So that I can have a log file to rummage through in the event of a problem, I start by configuring the CPAN client interactively, and then doing
$ cpan YAML 2>&1 | tee YAML.log
$ cpan Bundle::CPAN 2>&1 | tee YAML.log
Normally I would now install the modules specific to my use. But when I tried this time I got
HTTP::Tiny failed with an internal error: IO::Socket::SSL 1.42 must be installed for https support
Net::SSLeay 1.49 must be installed for https support
Spoiler Alert: This weekly challenge deadline is due in a few days from now (on March 26, 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: Special Bit Characters
You are given an array of binary bits that ends with 0.
Valid sequences in the bit string are:
[0] -decodes-to-> "a"
[1, 0] -> "b"
[1, 1] -> "c"
Write a script to print 1 if the last character is an “a” otherwise print 0.
For several years, I spent much time writing code for the Raspberry Pi, including hardware level register C code so that we can use various Integrated Circuit chips and sensors with Perl.
A couple of years ago, I acquired much larger and much more expensive toy, an all-wheel drive, full auto-pilot Tesla Model-X SUV, so of course, I want to write Perl code to access and manipulate it.
In the ensuing two years, I developed several microcontroller-based devices for the car, including one that knows where the car is, and its battery charge and state, and dispslays this information via an LED light strip and an OLED screen inside of my garage, along with an audible alarm that sounds for 1/8th of a second every three seconds if the battery is below a certain threshold so I don't forget to plug the charger in.
Dancer2 0.400000 has been released, and is on its way to CPAN.
We realize that some of you might be curious as to the large version bump. There are a couple of reasons for this:
- Modules we depend on bumped their minimum Perl version to 5.12, requiring us to follow suit.
- As of 2022, Dancer2 has an official deprecation policy. We are implementing this policy effective with this release, and it will help shape and guide future development.
- We’ve officially marked a lot of outdated and unused API as being deprecated.
With that, the following APIs, methods, etc. are now officially deprecated:
Dancer2::Test
request->dispatch_path
push_header
header
headers
context
Named placeholders: splat and capture
In plugins:
plugin_setting
dancer_app
request
var
hook
To discuss any of these, you can find issue for each of the above here.
Spoiler Alert: This weekly challenge deadline is due in a few days from now (on March 19, 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: Minimum Index Sum
You are given two arrays of strings.
Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
Around the beginning of 2022 I started noticing a large number of warnings when compiling XS modules under macOS 12 Monterey. These looked like warning: '(' and '{' tokens introducing statement expression appear in different macro expansion contexts [-Wcompound-token-split-by-macro], and appeared to originate fairly deeply in Perl's macro stack.
This week I was moved to address them for my one lone XS distribution, Mac-Pasteboard. Not only are they really annoying, but they would make it difficult or impossible to find anything more serious.
A little web searching seemed to say that this warning was added in clang 12.0, and is enabled by default. Beyond that, I did not find much. A Ruby ticket turned up, but the patch involved rewriting the relevant macros so that the warning was not tickled. A desultory check of a few other XS modules that came to mind did not provide any help -- they all showed the same behavior.
As a bystander in the evolution of Object-Oriented Programming in Perl,
and someone who is really only just starting to get the hang of Perl modules and packages
(still not any good at it), I get really quite overwhelmed by ideologies.
There is considerable debate about the right way to program things, the right
style, the right direction that Perl should go. It is Vim vs Emacs, Atari ST
vs Amiga, Mods vs Punks. Really one needs a language to do what one needs it
to do, simply, quickly and consistently. For an amateur, Perl has been able
to do exactly that for me. I code rubbishly, but hey, who's looking?