note
kcott
<p>G'day [govindkailas],</p>
<p>
It's generally not a good idea to export by default (i.e. with <c>@EXPORT</c>) — see the [http://perldoc.perl.org/Exporter.html|Exporter] documentation sections: [http://perldoc.perl.org/Exporter.html#Selecting-What-to-Export|Selecting What to Export] and [http://perldoc.perl.org/Exporter.html#What-Not-to-Export|What Not to Export]. Instead, consider using <c>@EXPORT_OK</c> and <c>%EXPORT_TAGS</c>.
</p>
<p>
Here's an example of <c>Utilities.pm</c> that does that:
</p>
<code>
package Utilities;
use strict;
use warnings;
use Exporter qw{import};
my @sys_exports = qw{$SYS_DIR $SYS_OTHER};
my @util_exports = qw{$UTIL_THIS $UTIL_THAT};
our @EXPORT_OK = (@sys_exports, @util_exports);
our %EXPORT_TAGS = (SYS => [@sys_exports], UTIL => [@util_exports]);
our $SYS_DIR = "sys_dir";
our $SYS_OTHER = "sys_other";
our $UTIL_THIS = "util_this";
our $UTIL_THAT = "util_that";
1;
</code>
<p>Here's some sample runs showing how to use it.</p>
<p>No namespace pollution unless specifically requested:</p>
<code>
$ perl -Mstrict -Mwarnings -E '
use Utilities;
say $SYS_DIR;
'
Global symbol "$SYS_DIR" requires explicit package name at -e line 3.
Execution of -e aborted due to compilation errors.
</code>
<p>Request a specific item:</p>
<code>
$ perl -Mstrict -Mwarnings -E '
use Utilities qw{$SYS_DIR};
say $SYS_DIR;
'
sys_dir
</code>
<p>Request multiple items using a tag:</p>
<code>
$ perl -Mstrict -Mwarnings -E '
use Utilities qw{:SYS};
say $SYS_DIR;
'
sys_dir
</code>
<p>Mix specific and tag requests:</p>
<code>
$ perl -Mstrict -Mwarnings -E '
use Utilities qw{:UTIL $SYS_OTHER};
say $SYS_OTHER;
say $UTIL_THAT;
'
sys_other
util_that
</code>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-861371">
<p>-- Ken</p>
</div></div>
1046340
1046340