Exporter-Auto

 view release on metacpan or  search on metacpan

lib/Exporter/Auto.pm  view on Meta::CPAN

package Exporter::Auto;
use strict;
use warnings;
use 5.008005;
our $VERSION = '0.04';

use Sub::Identify qw(stash_name);
use B::Hooks::EndOfScope;
use Exporter;

sub import {
    my $klass = caller(0);

    no strict 'refs';
    unshift @{"${klass}::ISA"}, 'Exporter';

    on_scope_end {
        my %hash = %{"${klass}::"};
        while (my ($k, $v) = each %hash) {
            next if $k =~ /^(?:BEGIN|CHECK|END|INIT|UNITCHECK)$/;
            next if $k =~ /^_/;
            next unless *{"${klass}::${k}"}{CODE};
            next if $klass ne stash_name($klass->can($k));
            push @{"${klass}::EXPORT"}, $k;
        }
    };
}

1;
__END__

=encoding utf8

=head1 NAME

Exporter::Auto - export all public functions from your package

=head1 SYNOPSIS

    package Foo;
    use Exporter::Auto;

    sub foo { }

    package main;
    use Foo;
    foo();  # <= this function was exported!

=head1 DESCRIPTION

Exporter::Auto is a simple replacement for L<Exporter> that will export
all public functions from your package. If you want all functions to be
exported from your module by default, then this might be the module for you.
If you only want some functions exported, or want tags, or to export variables,
then you should look at one of the other Exporter modules (L</"SEE ALSO">).

Let's say you have a library module with three functions, all of which
you want to export by default. With L<Exporter>, you'd write something like:

    package MyLibrary;
    use parent 'Exporter';
    our @EXPORT = qw/ foo bar baz /;
    sub foo { ... }
    sub bar { ... }
    sub baz { ... }
    1;

Every time you add a new function,
you must remember to add it to C<@EXPORT>.
Not a big hassle, but a small inconvenience.

With C<Exporter::Auto> you just write:

    package MyLibrary;
    use Exporter::Auto;
    sub foo { ... }
    sub bar { ... }
    sub baz { ... }
    1;



( run in 1.495 second using v1.01-cache-2.11-cpan-39bf76dae61 )