| 1 | package constant;
|
|---|
| 2 |
|
|---|
| 3 | use strict;
|
|---|
| 4 | use 5.006_00;
|
|---|
| 5 | use warnings::register;
|
|---|
| 6 |
|
|---|
| 7 | our($VERSION, %declared);
|
|---|
| 8 | $VERSION = '1.05';
|
|---|
| 9 |
|
|---|
| 10 | #=======================================================================
|
|---|
| 11 |
|
|---|
| 12 | # Some names are evil choices.
|
|---|
| 13 | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD };
|
|---|
| 14 |
|
|---|
| 15 | my %forced_into_main = map +($_, 1),
|
|---|
| 16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG };
|
|---|
| 17 |
|
|---|
| 18 | my %forbidden = (%keywords, %forced_into_main);
|
|---|
| 19 |
|
|---|
| 20 | #=======================================================================
|
|---|
| 21 | # import() - import symbols into user's namespace
|
|---|
| 22 | #
|
|---|
| 23 | # What we actually do is define a function in the caller's namespace
|
|---|
| 24 | # which returns the value. The function we create will normally
|
|---|
| 25 | # be inlined as a constant, thereby avoiding further sub calling
|
|---|
| 26 | # overhead.
|
|---|
| 27 | #=======================================================================
|
|---|
| 28 | sub import {
|
|---|
| 29 | my $class = shift;
|
|---|
| 30 | return unless @_; # Ignore 'use constant;'
|
|---|
| 31 | my %constants = ();
|
|---|
| 32 | my $multiple = ref $_[0];
|
|---|
| 33 |
|
|---|
| 34 | if ( $multiple ) {
|
|---|
| 35 | if (ref $_[0] ne 'HASH') {
|
|---|
| 36 | require Carp;
|
|---|
| 37 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'");
|
|---|
| 38 | }
|
|---|
| 39 | %constants = %{+shift};
|
|---|
| 40 | } else {
|
|---|
| 41 | $constants{+shift} = undef;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
|
|---|