I can not understand what is the difference/use case of EXPORT_OK
vs EXPORT
.
Most resources mentions something in the lines of:
@Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access its members.
@EXPORT_OK does export of symbols on demand basis for selective list of symbols (subroutines and variables) of the module.
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?
Let's say I have a package
MyPackage
that uses@EXPORT
.Now, when I use
MyPackage
in my code,do_awesome_thing
gets automatically exported to my code fromMyPackage
, 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).On the other hand, if I have a package
MyOtherPackage
that uses@EXPORT_OK
,and then try
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 saiduse MyOtherPackage
without explicitly asking fordo_awesome_thing
to be imported here, it doesn't get imported, and is accessible only by specifying the package name.The way you ask for
do_awesome_thing
to be imported is to sayuse MyOtherPackage qw(do_awesome_thing)
in the second line ofmynewscript.pl
above. This says import that module and makedo_awesome_thing
available directly. After that, the fourth line inmynewscript.pl
above will start working.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, onlydo_awesome_thing
will be. So, except for the default case ofuse 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.From the fine Exporter manual:
So, if you use
@EXPORT
and someone does the usualuse YourModule;
, then you've just polluted their namespace with everything in@EXPORT
. But, if you use@EXPORT_OK
, they have to specifically ask for things to be imported so the person using your module has control over what happens to their namespace.The difference is really a matter of who controls what gets into the
use
r's namespace: if you use@EXPORT
then the module beinguse
d does, if you use@EXPORT_OK
then the code doing the import controls their own namespace.Of course, you could always say
use Whatever();
to keep impolite modules from polluting your namespace but that's ugly and you shouldn't have to kludge around rude code that wants to scribble all over your namespace.