Sometimes, we want to initialize a bunch of variables with similar content. Declaring a set of values independently is a simple viable solution. This story is highly inspired from this StackOverflow question.

my $day  = 0;
my $mon  = 0;
my $year = 0;
my $hr   = 0;
my $min  = 0;
my $sec  = 0;

Ok, that’s kind of awful, they should all share the same value so using a shared constant might be a better idea:

use constant COMMON_INIT_VALUE => 0;

my $day  = COMMON_INIT_VALUE;
my $mon  = COMMON_INIT_VALUE;
my $year = COMMON_INIT_VALUE;
my $hr   = COMMON_INIT_VALUE;
my $min  = COMMON_INIT_VALUE;
my $sec  = COMMON_INIT_VALUE;

But it looks like a lot of typing and not very “smart”. Perl is good at one liners. So let’s try another strategy.

use constant COMMON_INIT_VALUE => 0;

my ($day, $mon, $year, $hr, $min, $sec) = (COMMON_INIT_VALUE) x 6;

Waaahoo, this is the kind of magic Perl is able to achieve, thanks to the repetition x operator. Almost perfect, but there is an annoying detail: we have to count the number of items. A “smarter”, and harder to maintain, therefore “unfriendly for beginners” version could be:

use constant COMMON_INIT_VALUE => 0;

map { ($_) =  COMMON_INIT_VALUE } my ($day, $mon, $year, $hr, $min, $sec);

…all credits to Scott Pritchett for this solution. I appreciate that it does not have the explicit 6 count, that’s really very elegant. But it implies some extra opaque code smartness that surely cannot be tolerated in every codebase.