本文介绍了在Perl中导出vs export_ok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白EXPORT_OKEXPORT的区别/用例是什么.
大多数资源在以下几行中都提到了一些东西:

I can not understand what is the difference/use case of EXPORT_OK vs EXPORT.
Most resources mentions something in the lines of:

但是我真的看不到这里的区别/含义.
有人可以提供这两个符号的区别/用途的基本例子吗?

But I really don't see the difference/meaning here.
Can someone please provide a small fundamental example of the difference/usage of these 2 symbols?

推荐答案

假设我有一个使用@EXPORT的软件包MyPackage.

Let's say I have a package MyPackage that uses @EXPORT.

#this is MyPackage.pm
package MyPackage;
@EXPORT = qw(do_awesome_thing);

sub do_awesome_thing { ... }

sub be_awesome { ... }

现在,当我在代码中使用MyPackage时,

Now, when I use MyPackage in my code,

#this is myscript.pl
use MyPackage;

do_awesome_thing(); #works

be_awesome(); #doesn't work
MyPackage::be_awesome(); #works

do_awesome_thing会自动从MyPackage导出到我的代码,而无需我说把这个给我". be_awesome不会被导出(也不会与@EXPORT_OK一起导出,我只是在演示该部分以使您清楚导出"给了我们什么).

do_awesome_thing gets automatically exported to my code from MyPackage, without me having to say "give this to me". be_awesome isn't exported (and it won't be exported with @EXPORT_OK either, I'm just showing that part to get you clear on what "exporting" gives us).

另一方面,如果我有使用@EXPORT_OK的软件包MyOtherPackage

On the other hand, if I have a package MyOtherPackage that uses @EXPORT_OK,

#this is MyOtherPackage.pm
package MyOtherPackage;
@EXPORT_OK = qw(do_awesome_thing);

sub do_awesome_thing { ... }

sub be_awesome { ... }

然后尝试

#this is mynewscript.pl
use MyOtherPackage;

do_awesome_thing(); #doesn't work
MyOtherPackage::do_awesome_thing(); #works, as always

直接调用do_awesome_thing的行将不起作用.这是因为在@EXPORT_OK中放入内容表示仅在用户要求时将其提供给我的用户".由于我们只是说use MyOtherPackage而没有在这里明确要求导入do_awesome_thing,因此它不会被导入,并且只能通过指定程序包名称来访问.

the line calling do_awesome_thing directly won't work. This is because putting something in @EXPORT_OK says "give this to my users only if they ask for it". Since we've just said use MyOtherPackage without explicitly asking for do_awesome_thing to be imported here, it doesn't get imported, and is accessible only by specifying the package name.

您要求导入do_awesome_thing的方式是在上面mynewscript.pl的第二行中说use MyOtherPackage qw(do_awesome_thing).这就是说导入该模块并使do_awesome_thing直接可用.之后,上面mynewscript.pl中的第四行将开始工作.

The way you ask for do_awesome_thing to be imported is to say use MyOtherPackage qw(do_awesome_thing) in the second line of mynewscript.pl above. This says import that module and make do_awesome_thing available directly. After that, the fourth line in mynewscript.pl above will start working.

请注意,用户也可以在第一个程序包中指定use MyPackage qw(do_awesome_thing),在这种情况下,不会导出@EXPORT列表中的任何其他内容,而只会导出do_awesome_thing.因此,除了use PackageName;的默认情况之外,@EXPORT@EXPORT_OK的行为类似.在默认情况下,@EXPORT中的任何内容都会自动引发到用户的脚本中,而@EXPORT_OK则更为礼貌并且不会导出任何内容.

Note that the user can specify use MyPackage qw(do_awesome_thing) with the first package also, and in that case, anything else in the @EXPORT list won't be exported, only do_awesome_thing will be. So, except for the default case of use PackageName;, @EXPORT and @EXPORT_OK behave similarly. In the default case, anything in @EXPORT gets thrown into the user's script automatically, while @EXPORT_OK is more polite and doesn't export anything.

这篇关于在Perl中导出vs export_ok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 13:59
查看更多