[ACCEPTED]-How can I export all subs in a Perl package?-subroutine

Accepted answer
Score: 24

Don't do any exporting at all, and don't 3 declare a package name in your library. Just 2 load the file with require and everything will 1 be in the current package. Easy peasy.

Score: 10

Don't. But if you really want to... write 2 a custom import that walks the symbol table and 1 export all the named subroutines.

# Export all subs in package. Not for use in production code!
sub import {
    no strict 'refs';

    my $caller = caller;

    while (my ($name, $symbol) = each %{__PACKAGE__ . '::'}) {
        next if      $name eq 'BEGIN';   # don't export BEGIN blocks
        next if      $name eq 'import';  # don't export this sub
        next unless *{$symbol}{CODE};    # export subs only

        my $imported = $caller . '::' . $name;
        *{ $imported } = \*{ $symbol };
    }
}
Score: 3

Warning, the code following is as bad an 3 idea as exporting everything:

package Expo;

use base "Exporter";

seek DATA, 0, 0; #move DATA back to package

#read this file looking for sub names
our @EXPORT = map { /^sub\s+([^({\s]+)/ ? $1 : () } <DATA>;

my $sub = sub {}; #make sure anon funcs aren't grabbed

sub foo($) {
    print shift, "\n";
}

sub bar ($) {
    print shift, "\n";
}

sub baz{
    print shift,"\n";
}

sub quux {
    print shift,"\n";
}

1;

__DATA__

Here is the 2 some code that uses the module:

#!/usr/bin/perl

use strict;
use warnings;

use Expo;

print map { "[$_]\n" } @Expo::EXPORT;

foo("foo");
bar("bar");
baz("baz");
quux("quux");

And here 1 is its output:

[foo]
[bar]
[baz]
[quux]
foo
bar
baz
quux
Score: 2

You can always call subroutines in there 19 fully-specified form:

MyModule::firstsub();

For modules I write 18 internally, I find this convention works 17 fairly well. It's a bit more typing, but 16 tends to be better documentation.

Take a 15 look at perldoc perlmod for more information about what 14 you are trying to accomplish.

More generally, you 13 could look at Exporter's code and see how it uses 12 glob aliasing. Or you can examine your 11 module's namespace and export each subroutine. (I 10 don't care to search for how to do that 9 at the moment, but Perl makes this fairly 8 easy.) Or you could just stick your subroutines 7 in the main package:

 package main;
 sub firstsub() { ... }

(I don't think that's a 6 good idea, but you know better than I do 5 what you are trying to accomplish.)

There's 4 nothing wrong with doing this provided you know 3 what you are doing and aren't just trying 2 to avoid thinking about your interface to 1 the outside world.

Score: 2

Perhaps you would be interested in one of 4 the Export* modules on CPAN that lets you 3 mark subs as exportable simply by adding 2 an attribute to the sub definition? (Don't 1 remember which one it was, though.)

Score: 2

https://metacpan.org/pod/Exporter::Auto

Exporter::Auto. this is all you need.

0

Score: 1

Although it is not usually wise to dump 9 all subs from module into the caller namespace, it 8 is sometimes useful (and more DRY!) to automatically 7 generate @EXPORT_OK and %EXPORT_TAGS variables.

The easiest method 6 is to extend the Exporter. A simple example 5 is something like this:

package Exporter::AutoOkay;
#
#   Automatically add all subroutines from caller package into the
#   @EXPORT_OK array. In the package use like Exporter, f.ex.:
#
#       use parent 'Exporter::AutoOkay';
#
use warnings;
use strict;
no strict 'refs';

require Exporter;

sub import {
    my $package = $_[0].'::';

    # Get the list of exportable items
    my @export_ok = (@{$package.'EXPORT_OK'});

    # Automatically add all subroutines from package into the list
    foreach (keys %{$package}) {
        next unless defined &{$package.$_};
        push @export_ok, $_;
    }

    # Set variable ready for Exporter
    @{$package.'EXPORT_OK'} = @export_ok;

    # Let Exporter do the rest
    goto &Exporter::import;
}

1;

Note the use of goto that 4 removes us from the caller stack.

A more 3 complete example can be found here: http://pastebin.com/Z1QWzcpZ It 2 automatically generates tag groups from 1 subroutine prefixes.

Score: 1

case 1

Library is :

package mycommon;

use strict;
use warnings;

sub onefunctionthatyoumadeonlibary() {
}
1;

you can use it, calling common:: :

#!/usr/bin/perl
use strict;
use warnings;
use mycommon;

common::onefunctionthatyoumadeonlibary()

case 2

Library 7 is , yousimple export them :

package mycommon;

use strict;
use warnings;

use base 'Exporter';

our @EXPORT = qw(onefunctionthatyoumadeonlibary);
sub onefunctionthatyoumadeonlibary() {
}
1;

use it in same 6 "namespace":

#!/usr/bin/perl
use strict;
use warnings;
use mycommon qw(onefunctionthatyoumadeonlibary);

onefunctionthatyoumadeonlibary()

Also we can do a mix of this 5 two cases , we can export more common functions 4 to use it without calling the packages name 3 and other functions that we only call it 2 with package name and that ones don't need 1 to be exported.

More Related questions