Pangram verdict · v3.3
We believe that this entire text is human-written.
AI likelihood · overall
HumanArticle text · 731 words · 1 segments analyzed
~/posts / sort-your-perl-importsby Olaf Alders · September 10, 2026 ·4 min read ·740 wordsI’ve had some time to revisit perlimports, and aside from fixing some bugs, I’ve finally added sorting. This brings it more in line with import tidiers in other languages. If you like to see your list of imported libraries in alpha-sorted order, you’re in the right place. If you’d like them sorted in order of width, you know where the door is!"Library" by Ellen Forsyth is licensed under CC BY-SA 2.0.Let’s see it in action. I asked claude to come up with a convoluted example.#Before and after Contains LLM-generated content not written by Olaf Before:package Report; use warnings; # ___ # ( o.o ) is this row an object # > ^ < or a plain number? use Scalar::Util qw( blessed ); use Carp qw( croak ); # fatal, with a caller-aware stack use feature 'signatures'; use List::Util qw( sum0 max ); use strict; use POSIX qw( floor ); # round takings down to whole dollars sub total ( $class, @rows ) { croak 'need rows' unless @rows; my @amounts = map { blessed($_) ? $_->amount : $_ } @rows; return floor( sum0(@amounts) ), max(@amounts); } 1; // end LLM-generated content After:package Report; use warnings; use feature 'signatures'; use strict; use Carp qw( croak ); # fatal, with a caller-aware stack use List::Util qw( max sum0 ); use POSIX qw( floor ); # round takings down to whole dollars # ___ # ( o.o ) is this row an object # > ^ < or a plain number? use Scalar::Util qw( blessed ); sub total ( $class, @rows ) { croak 'need rows' unless @rows; my @amounts = map { blessed($_) ? $_->amount : $_ } @rows; return floor( sum0(@amounts) ), max(@amounts); } 1; What happened:the feature and strict pragmas were hoisted out of the list belowthey were not sorted, but I’m ok with this because pragma order can matterthe remaining contiguous imports were alpha-sortedthe List::Util import list was sorted, but that’s pre-existing behaviourcomments remain attached to their imports#Running it from the command lineWe can preview the sorted result on stdout:perlimports --sort --filename Report.pm package Report; use warnings; use feature 'signatures'; use strict; use Carp qw( croak ); # fatal, with a caller-aware stack use List::Util qw( max sum0 ); use POSIX qw( floor ); # round takings down to whole dollars # ___ # ( o.o ) is this row an object # > ^ < or a plain number? use Scalar::Util qw( blessed ); sub total ( $class, @rows ) { croak 'need rows' unless @rows; my @amounts = map { blessed($_) ? $_->amount : $_ } @rows; return floor( sum0(@amounts) ), max(@amounts); } 1; We can rewrite the file in place (no output; it edits Report.pm and exits 0):perlimports --sort -i Report.pm We can check without rewriting — --lint reports what would be changed and then exits non-zero if anything needs tidying. So, it’s helpful in CI.perlimports --sort --lint --filename Report.pm ❌ Report.pm (includes are not sorted) @@ -4,5 +3,0 @@ -# ___ -# ( o.o ) is this row an object -# > ^ < or a plain number? -use Scalar::Util qw( blessed ); -use Carp qw( croak ); # fatal, with a caller-aware stack @@ -9,0 +5,3 @@ +use strict; + +use Carp qw( croak ); # fatal, with a caller-aware stack @@ -14 +11,0 @@ -use strict; @@ -15,0 +13,4 @@ +# ___ +# ( o.o ) is this row an object +# > ^ < or a plain number? +use Scalar::Util qw( blessed ); ❌ List::Util (import arguments need tidying) at Report.pm line 10 @@ -10,4 +10 @@ -use List::Util qw( +use List::Util qw( max sum0 ); - sum0 - max -); #Turning it on in perlimports.toml# perlimports.toml sort = true With sorting enabled in the config, we no longer need to pass the flag at the command line. This command still applies the sort:perlimports --filename Report.pm This is a first pass at this problem and I’m sure it does some things that people will hate and that I hadn’t considered. If you try it out and hate it, you have to let me know. Also, tell me on a scale of 1-10 how much you hate it, with 1 being “I threw up in my mouth a little” and 10 being “I wept”.Related posts:Getting Started with perlimportsDetective Work with perlimportsperlimports