I’m basing the majority on my Perl scripts on the following template/skeleton:
#!/usr/bin/perl -w
use strict;
use utf8;
$| = 1;
binmode(STDOUT, ":utf8");
# my code goes here.
The things achieved by this template:
- Enable warnings (
-w) - Enabling strict mode (
use strict) - Going pure UTF-8 (
use utf8+binmode(STDOUT, ":utf8")) - Disable buffering (
$| = 1)
My question is:
How can my template be improved to better reflect Perl best-practices?
Replace the
-wwithuse warnings. It allows you to disable warnings lexically should you need to. See perllexwarn.The
use utf8pragma is for when your source code is in UTF-8. If it is, great. If not… I don’t recommend adding things that you don’t actually use. Similarly, don’t set STDOUT to UTF-8 unless you’re actually producing it.Disabling buffering will reduce performance. Don’t do it unless you need to, and then limit the scope to the block where it’s necessary.
I like to include a statement to explicitly state the minimum version of Perl required to run the script. This makes the error messages more meaningful if it doesn’t compile due to someone using an older version of Perl. e.g.
I use that particular incantation instead of something like
use v5.8.1because it’s backwards-compatible with the versions of Perl I’m trying to “support” with a meaningful error message.