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
featureandstrictpragmas 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::Utilimport 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: