The Simplest Exporter?

I know this package is on the CPAN, but I can't find it. I'm revising some training material for my Perl testing course and want a simpler exporter primarily so that it can fit on my slides easier. What I currently have is:

package Some::Package;
use Ovid::Exporter qw(sub1 sub2);

sub sub1 { ... }
sub sub2 { ... }

And you would use it as you would expect:

use Some::Package qw(sub1 sub2);
# or
use Some::Package ':all';

The code looks like this:

package Ovid::Exporter;

use strict;
use warnings;
use Carp ();
require Exporter;

sub import {
    my ( $class, @subs ) = @_;
    unless (@subs) {
        Carp::croak("$class requires a list of subs to import");
    }
    my $calling_package = caller;

    no strict 'refs';
    push @{"${calling_package}::ISA"} => 'Exporter';
    @{"${calling_package}::EXPORT_OK"} = @subs;
    %{"${calling_package}::EXPORT_OK"} = ( all => \@subs );
}

1;

It's dirt simple and doesn't require any work to think about or any non-core dependencies. It also fits my training needs perfectly, but I'm sure it already exists on some dusty corner of the CPAN. Where would I find it?

5 Comments

Looks like Exporter::Easiest would allow you to do:

use Exporter::Easiest OK => qw( sub1 sub2 );

close enough?

use Exporter 'import';
our @EXPORT = qw(sub1 sub2);

I've been meaning to add something along these lines to Exporter::Tiny for a while. So I've done a trial release which includes a module called Exporter::Shiny that does more or less what Ovid::Exporter does in your example. Major differences:

  • Inherits from Exporter::Tiny instead of Exporter. This allows people importing functions to, for example, rename them.
  • Adds an entry to %INC for the caller module, if there isn't one there already. This just seemed handy.

I'll release a stable version later today or tomorrow, depending on how CPAN testers get along.

Leave a comment

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/