Skip to content

Instantly share code, notes, and snippets.

@robin-a-meade
Last active April 29, 2023 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin-a-meade/a933634278de902b4731056e764a9061 to your computer and use it in GitHub Desktop.
Save robin-a-meade/a933634278de902b4731056e764a9061 to your computer and use it in GitHub Desktop.
perl template boilerplate
#############################################################################
# BEGIN MODERN PERL BOILERPLATE #
#############################################################################
# Use perl v5.16.3
# (We're tracking RHEL7 as our baseline)
# Note:
# - `use v5.12` or higher implies `use strict`
# https://stackoverflow.com/q/6050031
# https://perldoc.perl.org/feature
# https://perldoc.perl.org/perl5120delta#Implicit-strictures
# Note: Standard Preamble redundantly does `use strict` anyway
# https://www.perl.com/pub/2012/04/perlunicook-standard-preamble.html/
# - But it doesn't imply `use warnings` --- that's not until v5.35
# - The matching feature bundle is loaded
# https://perldoc.perl.org/perlfunc#use-VERSION
# https://perldoc.perl.org/feature#FEATURE-BUNDLES
# https://stackoverflow.com/q/1878108
# - The 'v' is required if less than three components
# For example, this won't work: use 5.16
# But this will work: use 5.16.0
# - Otherwise, the 'v' is optional
# - Use of the 'v' is the only way in raku née perl 6
# https://docs.raku.org/language/pragmas
# https://gist.github.com/robin-a-meade/129bd1f6ed30f19d7ccb3e579beb5ec1
# - Thus it makes sense to use 'v' in the version spec. It is the modern way.
use v5.16.3;
# "All warnings are enabled automatically within the scope of a use v5.35 (or higher) declaration."
# https://perldoc.perl.org/warnings
# In the mean time, we enable all warning in the top level scope here:
use warnings;
use utf8; # Let literals and identifiers in this source code be in UTF-8
# Do the equivalent of -CD for the current lexical scope
#use open IN => ':encoding(UTF-8)';
#use open OUT => ':encoding(UTF-8)';
# More compactly:
#use open IO => ':encoding(UTF-8)';
# More compactly:
#use open ':utf8';
# Do the equivalent of -CSD for the current lexical scope
# (It should be noted that STDOUT, STDOUT, and STDERR are global, beyond the current lexical scope)
#binmode STDIN, ':encoding(UTF-8)';
#binmode STDOUT, ':encoding(UTF-8)';
#binmode STDERR, ':encoding(UTF-8)';
#use open IN => ':encoding(UTF-8)';
#use open OUT => ':encoding(UTF-8)';
# More compactly:
use open qw(:std :utf8);
# To be strict about valid code points and UTF-8 encoding,
# change :utf8 to :encoding(UTF-8)
# https://stackoverflow.com/q/14566460
# Or "fatalize" utf8 encoding warnings
# https://www.perl.com/pub/2012/04/perlunicook-standard-preamble.html/
use warnings qw(FATAL utf8); # fatalize encoding warnings
# You may relax some utf8 encoding warnings by disabling these sub warnings:
#no warnings "nonchar"; # The 66 forbidden non-characters
#no warnings "surrogate"; # UTF-16 surrogate code points
#no warnings "non_unicode"; # Codepoints over 0x10_FFFF
# https://perldoc.perl.org/perlunicook#%E2%84%9E-2:-Fine-tuning-Unicode-warnings
# Should we always be scrict about UTF-8?
# Sometimes it is convenient, or necessary, to not be.
# For example, when using an invalid code point as a sentinel delimiter.
# http://www.unicode.org/faq/private_use.html
# Todo: Examine how the above approaches behave in the presence of
# sentinels.
# Decode program arguments as UTF-8 (equivalent to the 'A' option letter to the -C flag)
# https://www.perl.com/pub/2012/04/perlunicookbook-decode-argv-as-utf8.html/
use Encode qw(decode_utf8);
@ARGV = map { decode_utf8($_, 1) } @ARGV;
# At this point we have the equivalent of -CSDA.
#############################################################################
# END MODERN PERL BOILERPLATE #
#############################################################################
say "Hi";
say "[$_]" foreach @ARGV;
# $foo; # This causes an error due to the implicit 'use strict'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment