# Sort Your Perl Imports

> Source: <https://www.olafalders.com/2026/09/10/sort-your-perl-imports/>
> Published: 2026-09-10 00:00:00+00:00

# Sort Your Perl Imports

## Table of Contents

I’ve had some time to revisit [perlimports](https://metacpan.org/pod/App::perlimports),
and aside from fixing some bugs, I’ve finally added [sorting](https://metacpan.org/pod/perlimports#-%5Bno-%5Dsort). This brings it more in
line with [import tidiers](https://pkg.go.dev/golang.org/x/tools/cmd/goimports) 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"](https://www.flickr.com/photos/30668772@N02/3910635234) by [Ellen Forsyth](https://www.flickr.com/photos/ellf/) is licensed under [CC BY-SA 2.0](https://creativecommons.org/licenses/by-sa/2.0/deed.en).

Let’s see it in action. I asked `claude` to come up with a convoluted example.

## [#](#before-and-after)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;
```

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 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)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
#    ___
#  ( 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-perlimportstoml)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:
