问题描述
我不明白EXPORT_OK
与EXPORT
的区别/用例是什么.
大多数资源在以下几行中都提到了一些东西:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!