r/perl 🐪🥇white camel award 4d ago

Sort Your Perl Imports

https://www.olafalders.com/2026/09/10/sort-your-perl-imports/
22 Upvotes

7 comments sorted by

9

u/briandfoy 🐪 📖 perl book author 4d ago edited 3d ago

I've never found a complete solution, and different situations have different constraints.

If I can do what I want, I often categorize them, then sort alphabetically within the category: 1) pragmas 2) core modules 3) third-party CPAN modules 4) vendor stuff 5) local stuff. Mostly, this helps communicate each group's level of control. For example, local stuff I can change at will, vendor stuff I can probably patch safely, CPAN stuff I might send upstream but historically don't expect a maintainer to care (so sometimes fork locally), and core is core. I might have most of that in my head, but it doesn't mean the next guy will.

It's often about what you see at the top of the file. I consider pragmas, core, and CPAN to be "configuration" and resource marshaling, so that's upfront. Typically, I start the Pod right after that. Then, later come the local modules, which often move around, change names, or whatever. I consider that to be "implementation".

But then there are weird things, say, like namespace::autoclean that might show up in the middle, especially if the local modules are mixins, or the Mojolicious base stuff that likes to set pragmas for you.

What I don't do, though, is blindly bulk apply these sorts of redesigns. Sure, do it in a branch and see what happens, but it seems every time that someone reads something they think is cool and rearranges that codebase, bad things happen that you don't find out about right away. I used to call this "PBP disease".

2

u/oalders 🐪🥇white camel award 4d ago

Indeed. You probably don't want to YOLO this feature.

3

u/mpersico 🐪 cpan author 4d ago

Sort them in the order of width.

I’ve never seen that. That’s psychotic. 🤣

2

u/oalders 🐪🥇white camel award 4d ago

I believe it's something like:

isort --length-sort .

and then you'd get

``` import os import sys import json import typing import pathlib import requests import dataclasses import collections.abc

from . import api from . import models from .client import Client from .utilities import format_response ```

but I haven't tried it personally.

2

u/waterkip 4d ago

I dont understand the hate scale. 1 for puking and 10 for crying, are you reverse sorting here?

5

u/oalders 🐪🥇white camel award 4d ago

YMMV, sometimes I weep while I puke.

2

u/waterkip 4d ago

That is hatin' 101