Memory Leak in Perl

After a very long time, I found time to blog and here is my journey to deal with memory leak in Perl.

https://theweeklychallenge.org/blog/memory-leak/

My Favorite Modules: Time::Piece

Time::Piece is a date/time module that replaces the built-in functions gmtime() and localtime(). The replaced functions return a Time::Piece object, with accessors for the compontents of the time. Time::Piece also provides formatting, parsing, and arithmetic.

This module has been in core since Perl 5.9.5. I was able to get it to pass tests as far back as 5.8.1, though not 5.8.0 or 5.6.2.

Without this module, you would obtain the current Gregorian year in your local zone like this

my $year = ( localtime() )[5] + 1900;

or maybe

my ( undef, undef, undef, undef, undef, $year ) = localtime();
$year += 1900;

Neither is particularly self-documenting, and the latter is much more verbose than we expect of Perl.

Perl Weekly Challenge 215: Odd One Out and Number Placement

These are some answers to the Week 215 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 May 7, 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: Odd One Out

You are given a list of words (alphabetic characters only) of same size.

Write a script to remove all words not sorted alphabetically and print the number of words in the list that are not alphabetically sorted.

Example 1

Input: @words = ('abc', 'xyz', 'tsu')
Output: 1

The words 'abc' and 'xyz' are sorted and can't be removed.
The word 'tsu' is not sorted and hence can be removed.

Example 2

Early bird discount expires 30th April for The Perl and Raku Conf 2022

Perl Foundation Banner_v3-01.jpg

Reminder: Until April 30th you can get your conference ticket for the low price of $275 per person! Register now!

Don't miss out on booking accommodation at the venue as it is certain to sell out.

Please like the Conference on Facebook and follow it on Twitter

TWC 160: Mystic/Math Balance

In which we pine for The Good Place, while visiting The Bad Place.

My Favorite Modules: Errno

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:

Perl Weekly Challenge 213: Fun Sort

These are some answers to the Week 213 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 April 23, 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: Fun Sort

You are given a list of positive integers.

Write a script to sort the all even integers first then all odds in ascending order.

Example 1

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

Example 2

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

Example 3

Input: @list = (1)
Output: (1)

Fun Sort in Raku

Installing Perl with perlbrew

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

echo source ~/perl5/perlbrew/etc/bash >> ~/.bashrc
or
echo source ~/perl5/perlbrew/etc/bash >> ~/.zshrc
depending on your shell.

Once installed, you can see a list of different installable Perl versions by issuing the

perlbrew available
command.

Perl Advent Calendar articles about Moose

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.

The ordering operators

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:

my ( $maj, $min, $rel ) = split /[.]/, $version;
my $version_ok = $maj > 3 || (
    $maj == 3 && ($min > 6 || (
        $min == 6 && $rel >= 8
    ))
);

Perl Weekly Challenge 212: Rearrange Groups

These are some answers to the Week 212 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 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.

Example 1

Начнём пожалуй

Про перл по-русски маловато пишут, вот и будем по возможности наполнять знаниями

Possible security problem in CPAN modules / Zlib CVE-2018-25032

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.

Making Dynamically Required Package Names More Discoverable in Perl

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.

Read the full article.

Perl Weekly Challenge 212: Jumping Letters

These are some answers to the Week 212 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 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.

Example 1

Installing Perl: A Chicken-and-Egg Problem

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

Please test DBD::Oracle v1.90_1

Anyone using DBD::Oracle is invited to test out out this dev release v1.90_1

Download from metacpan here

GH Tag here

v1.90 candidate branch

This commit is a big big change which should hopefully fix a lot of problems

Access the Tesla API with Perl!

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.

Perl Weekly Challenge 211: Toeplitz Matrix and Split Same Average

These are some answers to the Week 211 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Task 1: Toeplitz Matrix

You are given a matrix m x n.

Write a script to find out if the given matrix is Toeplitz Matrix.

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.

Example 1

Input: @matrix = [ [4, 3, 2, 1],
                   [5, 4, 3, 2],
                   [6, 5, 4, 3],
                 ]
Output: true

Example 2

Input: @matrix = [ [1, 2, 3],
                   [3, 2, 1],
                 ]
Output: false

Dancer2 0.400000 Released

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.

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.