2022 twenty-four merry days of Perl Feed

The Christmas Time Machine

Feature::Compat::Try - 2022-12-10

A Terrible Flashback

At the North Pole, the elves are just starting the Christmas 2022 preparations.

As usual they restarted doing line work, building toys, then sorting them per area. Everyone happily singing and working! When suddenly one elf put the first toy in a box marked "Salt Lake City" and was surprised it was not empty...

And he had a terrible flashback, they seemed to have forgotten to deliver the 2021 toys to Salt Lake City!

Go Ask Santa

The elves panicked, so they quickly reached out to Santa for a solution.

He took some time to think about it, then had an idea: Let's use the time machine to go back to 2021 and deliver the gifts!

(If you're wondering: YES, time machines do exist in North Pole! And I bet that elves even already used it in the past for similar mistakes? :P)

So a group of elves decided to use the time machine, fixed the arrival date to 25 December 2021, pushed the "go" button and zooooooooooooop!

Try to Deliver Back in the Past

Upon arriving in 2021, they immediately tried to run Perl software that they took with them.

For the record, in 2021, North Pole was equipped with Ubuntu 20.04 and here is the Perl code they had:

#!/usr/bin/perl

use feature 'try';
no warnings 'experimental::try';

try {
    deliver();
} catch($e) {
    print "Failed to deliver\n";
}

But it failed with:

    Feature "try" is not supported by Perl 5.30.0 at deliver.pl line 3.
    BEGIN failed--compilation aborted at deliver.pl line 3.

This is because the perl interpreter on which they wanted to run the code is older (5.30 in Ubuntu 20.04) than the one from where they are coming (5.36 in a rolling release distribution).

This is called "forward compatibility".

The Perl Time Machine

One of the elves had heard about the Perl "Time Machine" and was finally able to fix the Perl script thanks to Feature::Compat::Try:

#!/usr/bin/perl

use Feature::Compat::Try;

try {
    deliver();
} catch($e) {
    print "Failed to deliver\n";
}

This way, the CPAN module enables the feature try for capable interpreters or implements the feature try for older ones. It allowed the try feature to "travel" in the past with the elves and it also means that, back in 2022, elves would be able to reuse the updated version of the script without any change.

Thanks to this trick, the 2021 Christmas of Salt Lake City was saved! Ouf!

Credits to Paul Evans for Feature::Compat::Try and Perl's Amazing Time Machine

Gravatar Image This article contributed by: Thibault DUPONCHELLE <thibault.duponchelle@gmail.com>