Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl's Modules lists

I got confused with something while importing a module, like:

use POSIX;

&

use POSIX();

&

use POSIX qw(WNOHANG);

What is the difference between these use?

like image 885
Mahmoud Emam Avatar asked Oct 05 '13 09:10

Mahmoud Emam


1 Answers

Most modules use the Exporter module to expose functions/variables/constants in the callee's namespace.

use POSIX;

This will only import all symbols from POSIX's @EXPORT into the namespace of the calling module.

use POSIX();

This will not import any symbols into the calling namespace. It does however load the module, which means you can call functions like POSIX::strftime(...), etc.

use POSIX(WNOHANG)

This will only import the symbol WNOHANG into the calling module's namespace.

If you are not familiar with the @EXPORT and @EXPORT_OK arrays, you should definitely run through the documentation of Exporter. Using Exporter is the standard way in Perl to export symbols from one module into the namespace of your module (the calling namespace). POSIX uses it as well.

It's also probably worth mentioning that modules designed with an object-oriented interface, generally do not require symbols to be imported.

like image 174
Nikhil Avatar answered Nov 16 '22 08:11

Nikhil