2022 twenty-four merry days of Perl Feed

The Sleigh odometer

state - 2022-12-07

Sleigh Odometer

Customize the Sleigh

It's already December and elves are working hard to produce all of the toys for Christmas.

On the other side, Santa is preparing his sleigh, cleaning or customizing.

This year, in addition to a temperature sensor, Santa asked to install an odometer to his sleigh so that he can count each delivery. For the record, the distance unit is neither kilometers nor miles nor inches but "elf little toe"... but it does not change what follows :)

In order to implement the counter of the odometer, an elf came up with this code:

#!/usr/bin/env perl
use 5.010;

sub count() {
        my $distance = 0;
        return ++$distance;
}

count(); count(); count(); count();
say count();

But sadly it does not work as expected because the distance is reset at each count() call :/

Asking Experienced Elves

After asking some more experienced elves, the elf decided to implement a persistent variable via a "lexical trick":

#!/usr/bin/env perl
use 5.010;

{
    my $distance = 0;
    sub count {
        return ++$distance;
    }
}

count(); count(); count(); count();
say count();

And it worked!

Some other experienced elf proposed to use a "closure" that he perceived as "more elegant":

#!/usr/bin/env perl
use 5.010;

sub make_counter() {
    my $distance = 0;
    return sub {
        return ++$distance;
    }
}

my $counter = make_counter();

$counter->(); $counter->(); $counter->(); $counter->();
say $counter->();

Final Word from Santa

These 2 previous pieces of code work well, but there is definitely a simpler way!

As usual, the final word came from Santa who decided to use state:

#!/usr/bin/env perl
use 5.010;

sub count() {
        state $distance = 0;
        return ++$distance;
}

count(); count(); count(); count();

say count();

With this new "option", Santa felt upgraded and was able to know when to take a break or when to feed the reindeers!

And most important of all, it helped to arrive on time for the dinner prepared by Mrs Claus!

References

See Perl Maven "static and state variables in perl"

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