Monday 7 July 2014

Perl: Import and Export

@EXPORT_OK
This array contains symbols that can be imported if they are specifically asked for.

In the module, for example,
@EXPORT_OK = qw (Op_Func %Table);

The user could load the module like so
use YourModule qw(Op_Func %Table F1);
# The F1 function was listed in the @EXPORT array. Notice that this does not automatically import F2 or @List, even though they're in the @EXPORT array. To get everything in @EXPORT plus extras from @EXPORT_OK, use the special :DEFAULT tag, such as:
use YourModule qw(:DEFAULT %Table);

%EXPORT_ TAGS
This hash is used by large modules like CGI or POSIX to create higher-level groupings of related import symbols. Its values are references to arrays of symbol names, all of which must be in either @EXPORT or @EXPORT_OK. Here's a sample initalization:
%EXPORT_TAGS=(
    Functions=>[ qw (F1 F2 Op_Func) ],
    Variables=>[ qw (@List %Table) ]
);

An import symbol with a leading colon means to import a whole group of symbols. Here's an example:
use YourModule qw(:Functions %Table);

That pulls in all symbols from:
@{ $YourModule::EXPORT_TAGS{Functions} } and then the %Table hash.




No comments:

Post a Comment