Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Better way to define global variables

by kcott (Chancellor)
on Jul 25, 2013 at 14:36 UTC ( #1046360=note: print w/replies, xml ) Need Help??

in reply to Better way to define global variables

G'day govindkailas,

It's generally not a good idea to export by default (i.e. with @EXPORT) — see the Exporter documentation sections: Selecting What to Export and What Not to Export. Instead, consider using @EXPORT_OK and %EXPORT_TAGS.

Here's an example of that does that:

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;

Here's some sample runs showing how to use it.

No namespace pollution unless specifically requested:

$ 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.

Request a specific item:

$ perl -Mstrict -Mwarnings -E ' use Utilities qw{$SYS_DIR}; say $SYS_DIR; ' sys_dir

Request multiple items using a tag:

$ perl -Mstrict -Mwarnings -E ' use Utilities qw{:SYS}; say $SYS_DIR; ' sys_dir

Mix specific and tag requests:

$ perl -Mstrict -Mwarnings -E ' use Utilities qw{:UTIL $SYS_OTHER}; say $SYS_OTHER; say $UTIL_THAT; ' sys_other util_that

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046360]
[1nickt]: Good morning Monks and Monkettes!
[1nickt]: I have encountered a small code snippet I can not decipher: if ($?) { my $status = $? >> 8; ... }
[1nickt]: Hm, found the doc
[hippo]: >> is the bitwise shift, essentially divides the result by 256.
[haukex]: $? and system - the exit code of a process is stored in the upper bits of $?, so >> 8 does a bit shift to get at it
[hippo]: (truncating)
[erix]: aah, yes haukex++
[hippo]: Re: DBD::Sqlite queries slow - and gives wrong results has 6 upvotes. Is everyone quite well?
[erix]: hippo I think the 26 downvotes sick
[erix]: eh, 29 downvotes

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-12-13 11:34 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (359 votes). Check out past polls.