Perl 5.36

Perl 5.36 is out and it looks pretty great! Let's try it!

Debian bullseye provides Perl 5.32 and I've been just using that. Now, I think I will use plenv to install 5.36 and manage my Perl binaries in general.

 ❯ git clone https://github.com/tokuhirom/plenv.git ~/.plenv
 ...
 ❯ git clone https://github.com/tokuhirom/Perl-Build.git ~/.plenv/plugins/perl-build/
 ...

I already had the following in my .profile.d/perl.sh file

# Enable plenv.
if [ -d "$HOME/.plenv/bin" ] ; then
    PATH="$HOME/.plenv/bin:$PATH"
    eval "$(plenv init -)"
fi

so I just logged out and logged back in. Now plenv is ready to go!

 ❯ which plenv
 /home/tim/.plenv/bin/plenv

Install Perl 5.36!

 ❯ plenv install 5.36.0 -Dusethreads
 ...
 ❯ plenv global 5.36.0

 ❯ plenv versions
   system
 * 5.36.0 (set by /home/tim/.plenv/version)

 ❯ perl -v | head -2

 This is perl 5, version 36, subversion 0 (v5.36.0) built for x86_64-linux-thread-multi

Of course, we'll want cpanm and some CPAN modules!

 ❯ plenv install-cpanm
 ❯ plenv rehash
 ❯ cpanm Path::Tiny
 ❯ cpanm Term::ReadLine::Gnu
 ❯ cpanm Data::Printer

So what's new in Perl 5.36? Well, the biggest things to me are that subroutine signatures are no longer experimental and the warnings pragma is now implicit. Virtually all of my Perl programs start out with these four lines

#!/usr/bin/env perl

use v5.32;
use warnings;
use experimental qw(signatures);

Now I can just use two lines

#!/usr/bin/env perl

use v5.36;

and get the same behavior– hooray!

For example, here's a little program to check the PNG header on a file.

#!/usr/bin/env perl

use v5.36;

my $file = shift // die "\n\tUsage: $0 file\n";

if (is_png($file)) {
    say "$file is a PNG";
} else {
    say "$file isn't a PNG";
}

sub is_png($file) {
    # https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header
    state $PNG = pack "H*", "89504E470D0A1A0A";

    open my $fh, '<', $file or die "Cannot open file '$file': $!";

    my $n = read $fh, my $buffer, 8;

    die "Cannot read from file!" unless defined $n;
    die "Could not read 8 bytes!" unless $n == 8;

    if ($buffer eq $PNG) {
        return 1;
    } else {
        return 0;
    }
}