質問

I'm confused with the lines

use Exporter;
our @EXPORT =qw(a b);

in a package.

I've seen packages without using @EXPORT and work just fine.Pretty confused... I've googled around but didn't find much explanation.

My question is when should you use @EXPORT? And what's the reason for not using it?

役に立ちましたか?

解決

There are actually two questions there (or maybe you are only asking one but a full answer has to answer both).

When do I need to use Exporter

When you 'use' someone's perl module, two distinct actions are taken. The first is to load their package in; the second is to call their package's 'import' sub.

Just loading their package in results in the content of their package being defined within the namespace of their package. You could use it straight away, without the second step, but you would have to refer to all of it's subs etc using the full package name. For example if their package contained:

package Foo::Bar;

sub fabronicate {
    print "This is a nice sub!\n";
}

then anyone wanting to use if would have to do this:

use Foo::Bar;

Foo::Bar::fabronicate();

All that typing Foo::Bar in front of everything would get old real quick. A solution to this is to declare an 'import' sub in the package. That sub would have the responsibility of exporting the public subs (and variables and anything else the module should provide) into the namespace of the caller (the package that is 'use'ing the module).

What the Exporter module does is provide an 'import' sub for you. It means you don't have to write some strange looking code to create aliases for all of the symbols that are to be exported, it is all done for you.

Note that you don't have to use Exporter. You can write your own import routine, or there are alternative modules to handle exporting symbols.

Or you can just let people using your module explicitly mention the package name all the time. This is common in object-oriented modules, because it is only class methods (such as the constructors) which need to be explicitly prefixed with the package names. After you have an object instance, package lookup is automatic. Eg:

my $o = Foo::Bar->new();
$o->fabronicate();

When do I need to use @EXPORT

The Exporter module needs to know, of the symbols (subs, variables, etc) that are defined in the package, which ones should be exported. Normally only a small subset will be exported. It consults a few package variables to find this out: @EXPORT and @EXPORT_OK.

@EXPORT contains a list of symbols that you want to have exported to anyone who uses your module, they do not explicitly mention a list of symbols to import. Initially this sounds ok, until you realize that there will be problems if your modules is exporting some names which are also being exported by other modules.

@EXPORT_OK contains a list of symbols which can be exported, if the calling module specifically requests them. The calling module specifies these using a list of names after the module name, in the 'use' statement.

use Foo::Bar qw(fabronicate);

# Now I can call fabronicate directly!

Note that providing a list of symbols works either way: the big difference between @EXPORT and @EXPORT_OK is what happens when someone uses your module but does not provide any list, like this:

use Foo::Bar;

In this case, everything defined in @EXPORT is imported, and nothing defined in @EXPORT_OK is imported.

There are some other variables which allow you to specify tags which import multiple symbols. The Exporter documentation is the best place to look for this.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top