{"slug": "sort-your-perl-imports", "title": "Sort Your Perl Imports", "summary": "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.", "body_md": "# Sort Your Perl Imports\n\n## Table of Contents\n\nI’ve had some time to revisit [perlimports](https://metacpan.org/pod/App::perlimports),\nand aside from fixing some bugs, I’ve finally added [sorting](https://metacpan.org/pod/perlimports#-%5Bno-%5Dsort). This brings it more in\nline with [import tidiers](https://pkg.go.dev/golang.org/x/tools/cmd/goimports) in other languages. If you like to see your list of\nimported libraries in alpha-sorted order, you’re in the right place. If you’d\nlike them sorted in order of width, you know where the door is!\n\n[\"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).\n\nLet’s see it in action. I asked `claude` to come up with a convoluted example.\n\n## [#](#before-and-after)Before and after\n\n## Contains LLM-generated content not written by Olaf\n\nBefore:\n\n```\npackage Report;\n\nuse warnings;\n#    ___\n#  ( o.o )   is this row an object\n#   > ^ <    or a plain number?\nuse Scalar::Util qw( blessed );\nuse Carp qw( croak );    # fatal, with a caller-aware stack\nuse feature 'signatures';\nuse List::Util qw(\n    sum0\n    max\n);\nuse strict;\nuse POSIX qw( floor );   # round takings down to whole dollars\n\nsub total ( $class, @rows ) {\n    croak 'need rows' unless @rows;\n    my @amounts = map { blessed($_) ? $_->amount : $_ } @rows;\n    return floor( sum0(@amounts) ), max(@amounts);\n}\n\n1;\n```\n\nAfter:\n\n```\npackage Report;\n\nuse warnings;\nuse feature 'signatures';\nuse strict;\n\nuse Carp qw( croak );    # fatal, with a caller-aware stack\nuse List::Util qw( max sum0 );\nuse POSIX qw( floor );   # round takings down to whole dollars\n#    ___\n#  ( o.o )   is this row an object\n#   > ^ <    or a plain number?\nuse Scalar::Util qw( blessed );\n\nsub total ( $class, @rows ) {\n    croak 'need rows' unless @rows;\n    my @amounts = map { blessed($_) ? $_->amount : $_ } @rows;\n    return floor( sum0(@amounts) ), max(@amounts);\n}\n\n1;\n```\n\nWhat happened:\n\n- the `feature` and`strict` pragmas were hoisted out of the list below\n  - they were not sorted, but I’m ok with this because pragma order can matter\n- the remaining contiguous imports were alpha-sorted\n- the `List::Util` import list was sorted, but that’s pre-existing behaviour\n- comments remain attached to their imports\n\n## [#](#running-it-from-the-command-line)Running it from the command line\n\nWe can preview the sorted result on stdout:\n\n```\nperlimports --sort --filename Report.pm\npackage Report;\n\nuse warnings;\nuse feature 'signatures';\nuse strict;\n\nuse Carp qw( croak );    # fatal, with a caller-aware stack\nuse List::Util qw( max sum0 );\nuse POSIX qw( floor );   # round takings down to whole dollars\n#    ___\n#  ( o.o )   is this row an object\n#   > ^ <    or a plain number?\nuse Scalar::Util qw( blessed );\n\nsub total ( $class, @rows ) {\n    croak 'need rows' unless @rows;\n    my @amounts = map { blessed($_) ? $_->amount : $_ } @rows;\n    return floor( sum0(@amounts) ), max(@amounts);\n}\n\n1;\n```\n\nWe can rewrite the file in place (no output; it edits `Report.pm` and exits `0`):\n\n```\nperlimports --sort -i Report.pm\n```\n\nWe can check without rewriting — `--lint` reports what would be changed and\nthen exits non-zero if anything needs tidying. So, it’s helpful in CI.\n\n```\nperlimports --sort --lint --filename Report.pm\n❌ Report.pm (includes are not sorted)\n@@ -4,5 +3,0 @@\n-#    ___\n-#  ( o.o )   is this row an object\n-#   > ^ <    or a plain number?\n-use Scalar::Util qw( blessed );\n-use Carp qw( croak );    # fatal, with a caller-aware stack\n@@ -9,0 +5,3 @@\n+use strict;\n+\n+use Carp qw( croak );    # fatal, with a caller-aware stack\n@@ -14 +11,0 @@\n-use strict;\n@@ -15,0 +13,4 @@\n+#    ___\n+#  ( o.o )   is this row an object\n+#   > ^ <    or a plain number?\n+use Scalar::Util qw( blessed );\n\n❌ List::Util (import arguments need tidying) at Report.pm line 10\n@@ -10,4 +10 @@\n-use List::Util qw(\n+use List::Util qw( max sum0 );\n-    sum0\n-    max\n-);\n```\n\n## [#](#turning-it-on-in-perlimportstoml)Turning it on in perlimports.toml\n\n```\n# perlimports.toml\nsort = true\n```\n\nWith sorting enabled in the config, we no longer need to pass the flag at the command line. This command still applies the sort:\n\n```\nperlimports --filename Report.pm\n```\n\nThis 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”.\n\nRelated posts:", "url": "https://wpnews.pro/news/sort-your-perl-imports", "canonical_source": "https://www.olafalders.com/2026/09/10/sort-your-perl-imports/", "published_at": "2026-09-10 00:00:00+00:00", "updated_at": "2026-09-10 04:20:53.441150+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["perlimports", "Olaf Alders", "Perl", "List::Util", "goimports", "Claude"], "alternates": {"html": "https://wpnews.pro/news/sort-your-perl-imports", "markdown": "https://wpnews.pro/news/sort-your-perl-imports.md", "text": "https://wpnews.pro/news/sort-your-perl-imports.txt", "jsonld": "https://wpnews.pro/news/sort-your-perl-imports.jsonld"}}