| Subcribe via RSS

Perl Module Monday: AutoRole

August 9th, 2011 Posted in CPAN, Perl

This week’s choice is a sort of follow-up to last week’s post. AutoRole is a module that lets you do run-time or compile-time loading of modules, along with potentially renaming what you import on the fly. It takes these two useful features and wraps them into one package.

I try not to do two such closely-related picks back-to-back, but I had actually noticed AutoRole about a year ago, and taken note of it. It was shortly after I had taken RJBS’s Moose tutorial at OSCON ’10, and I was looking for a way to do roles at my day-job without having the luxury of a full-on Moose install. Alas, as often happens with my ADD-addled brain, I promptly forgot about the module after a few days. Luckily, the module’s author mentioned it in a comment to last week’s post, and it reminded me to take another look at it.

And it looks quite flexible. You can specify one of three methods for loading the module (compile-time or two flavors of run-time loading), and you can both specify the methods/routines to load, and give them alternate names if necessary (or just desired). It’s also quite light-weight and has no dependencies. If I were to have any concerns about it, it would be that it seems to be fairly young code (the latest version at this writing being 0.03) and hasn’t been updated in just over a year. BUT, and this is important, these two facts don’t mean the code isn’t solid and usable. I, too, have had code that reached the point where I felt it was feature-complete and stable, while only having reached version 0.4 or so. So don’t let these two factors prevent you from at least looking over AutoRole and maybe giving it a try.

One thing I did notice that Exporter does, that neither Sub::Exporter or AutoRole seem to do, is export variables themselves. However, this may be considered a feature by some!

So here you go: Sub::Exporter for creating more flexibility in your exports as a module developer, and AutoRole for more flexibility on your imports (and to be fair, I’ll mention that RJBS also has a module called Sub::Import that provides a Sub::Exporter-sort-of-flexibility when importing from modules that don’t use Sub::Exporter). Different approaches to the same sort of problem, depending on the angle from which you are approaching it. Myself, I’ll be looking at both modules for my own use, at home and at work.

4 Responses to “Perl Module Monday: AutoRole”

  1. Matt S TroutNo Gravatar Says:

    You might also want to look at Moo – which contains a Role::Tiny for ultimate minimalism.

    Plus Exporter::Declare is really rather nice too.

    – mst


  2. Caleb Cushing ( xenoterracide )No Gravatar Says:

    be nice to have a code example.


  3. rjrayNo Gravatar Says:

    Caleb: I agree, and I hope to do more along those lines in future posts. But sometimes (like last night) I have only a little time to write and still make it under the “Monday” wire. (And in fact, last night I failed by about 2 minutes!)

    mst: Thanks! I’ll take a look at Exporter::Declare, and maybe follow all of this up with a “shoot-out” sort of posting, that compares all of the solutions feature-by-feature.


  4. Paul SeamonsNo Gravatar Says:

    Cool modules.
    Main differences I can see is that Exporter::Declare is more akin to Sub::Exporter. Role::Tiny is closer to AutoRole. If you don’t need method renaming you can always just use autouse which has been core with perl for some time.

    Role::Tiny is nice but there are a few differences. AutoRole doesn’t load the remote module by default (unless explicitly asked or unless the list of imported methods is dynamic). Role::Tiny still requires being used in the imported class. AutoRole dies when there are method conflicts rather than silently ignoring them. To match the silent ignore method listed in the Role::Tiny POD where Role::Tiny choose to not import sub foo because it was already defined – you’d have to do something like this with AutoRole:

    use AutoRole ‘Some::Role’ => qr{^(?!foo$).};
    # imports all methods that don’t equal foo

    And yes AutoRole is still at .03 because there haven’t been any reasons to update it. Version .04 will probably remove internal use of strict and warnings since we haven’t had any reports of spurious warnings and it would cut out 5 or 6 lines (and autouse doesn’t use strict either).


Leave a Reply