Suggested update for Perl template

At the moment, Perl templates use all uppercase global variables, as in

select(STDOUT); $| = 1; # DO NOT REMOVE

chomp($N = <STDIN>);

How about replacing that with lexical, lowercase variables, along with enabling strict and warnings by default? Like so:

use strict;
use warnings;

select(STDOUT); $| = 1; # DO NOT REMOVE

chomp(my $n = <STDIN>);

You’re right, that something I found weird at first but I worked without it somehow (shame on me for not using these on CodinGame).

I’ll suggest it to start Perl templates with:

use strict;
use warnings;
#use diagnostics;

Before sending the suggestion to the team, what would you say about adding the following ?

use 5.20.1;

Good point with the use 5.20.1;, it’s not like we have to write backwards compatible code or something :wink:

I also think the way of reading in N lines of input using a C-style for loop is “unperlish” and would probably more typically be done with something like foreach my $i (1..$n), but that’s definitely more of a style issue. use strict, use warnings and lexicals on the other hand are a must, in my opinion.

Then I’ll write the suggestion this morning :wink:

I started tinkering around with Clash of Code and realized that there, I don’t want use strict, to save time when typing. Thought I’d add that here!

I agree, it does not really help for CoC.
However, we haven’t planned to propose different templates for the same language at the moment. So that’s either we propose a template that follow good practice in Perl, or… well we stay with the current template.

In my opinion, CodinGame should help developers to use and get used to good pratice when possible.

If there’s a choice of only one, then absolutely the warnings/strict one.

I keep coming back to this, but things keep popping up :wink: If use 5.12 or higher is included, use strict is implied and doesn’t have to be issued separately, see perldoc use.

1 Like

bewuethr are right, there is no need for ‘use strict’ if we have ‘use 5.20.1’

‘use 5.20.1’ already include ‘use strict’, we know that.
However, ‘use strict’ is a good practice for one who uses a pre v5.10 version of Perl. Writing it on in the template helps to not forget it if you are using an older version of Perl outside of CodinGame (sometimes for compatibility reason).

What about is to place ‘use 5.20.1;’ at the beginning?
IMO it is a more correct and common place.

You might also want to consider:

use Modern::Perl;

This turns on the practical defaults that we want to have. But you’ll need to install that package because it doesn’t work by default.