Sean O'Leary
Sean O'Leary

Reputation: 304

Perl package defined in script file, how to have package export subroutines?

Given the following:

package My::Pack;

use Exporter::Easy (
    OK => [ qw(pack_func) ],
);

sub pack_func {
    ...
}

package main;
<script here that uses pack_func>

How do I import the symbol I want?

use doesn't work since it's looking for another file to open. require doesn't support the syntax that use does for specifying the symbols you want.

Are my only choices to say My::Pac::pack_func() in the script or import the symbol manually through typeglob assignmnet?

Upvotes: 1

Views: 51

Answers (1)

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

The statement:

use Some::Module qw(foo bar);

is exactly equivalent to:

BEGIN {
    require Some::Module;
    Some::Module->import( qw(foo bar) );
}

in your case, the code for the My::Pack module has already been loaded, so you don't need to require it. Thus, you can just do:

BEGIN { My::Pack->import( qw(pack_func) ) }

Upvotes: 1

Related Questions