Clean up your Perl boilerplate with 'experimental'
I have a regular header that I use for new Perl things, carrying things that I might not even need, but will want.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature qw{ postderef say signatures state switch };
no warnings
qw{ experimental::postderef experimental::smartmatch experimental::signatures };
# real code starts here
I think, if you spend time with Perl 5, you’ll know most of that.
env perlmakes it easy to use your Perl, not system Perl.strictrestricts some of the more widely-abusable aspects of Perl, andwarningsgives you more control over what things Perl will complain about.featuregives you a bunch of the new Modern Perl things that we love. I like putting them in explicitly ratheruse Modern::Perloruse 5.30. (Is that the right syntax?)- But the experimental features tell you they’re experimental, so I use
no warningsto silence the “Hey, did you know that you’re using an experimental feature?” warnings.
But this morning, I was told about a better way.
Now, my boilerplate would look like:
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature qw{ say state };
use experimental qw{ postderef signatures switch } ;
# real code starts here
say (implicit newlines after print) and state (lexically-scoped variables) came in 5.10, so they’re not remotely experimental. But the others…
postderefallows me to use$arrayref->@*instead of@$arrayref, and$arrayref->[0]instead of$$arrayref[0], which I think are more clear.signaturesmean I can write subroutines likesub my_code ( $id , $count = 0 ) { ... }instead of having to pul these things out of@ARGVmyself.switchusessmartmatchwhich I haven’t liked to use explicitly, but I pull out switch statements just often enough for me to want to have it ready.
And now, instead of gimme experimental features; shut up about my experimental features;, there’s syntax that in one line.
It’s not a big change, granted, but it makes things look just that much nicer. I approve.