cd /news/developer-tools/sort-your-perl-imports · home topics developer-tools article
[ARTICLE · art-125395] src=olafalders.com ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

Sort Your Perl Imports

Olaf Alders added a sorting feature to perlimports, the Perl module that tidies use statements, bringing it in line with import tidiers such as Go's goimports. The new --sort flag alpha-sorts contiguous imports, hoists feature and strict pragmas out of the sorted list, keeps comments attached to their imports, and sorts List::Util import arguments as before. The tool can preview the result on stdout, rewrite files in place with -i, or report needed changes via --lint and exit non-zero for CI use.

read4 min views1 publishedSep 10, 2026
Sort Your Perl Imports
Image: Olafalders (auto-discovered)

Table of Contents #

I’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;
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;

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
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 andstrict pragmas were hoisted out of the list below
    • they were not sorted, but I’m ok with this because pragma order can matter
  • the remaining contiguous imports were alpha-sorted
  • the List::Util import list was sorted, but that’s pre-existing behaviour
  • comments remain attached to their imports

#Running it from the command line #

We 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
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 #

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:

── more in #developer-tools 4 stories · sorted by recency
── more on @perlimports 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/sort-your-perl-impor…] indexed:0 read:4min 2026-09-10 ·