S
Steve Allan
I'm creating a module for the sole purpose of defining constants to be
shared among several Perl scripts. I'm looking for a way to simplify
getting all the constant names into the @EXPORT_OK array, so I don't
end up with this:
@EXPORT_OK = qw(CURRENTCHANGE BLDNUM STAGEROOTDIR ...);
because I can envision that list becoming large, and everytime I
define a new constant I have to remember to add it to the list.
Here's what I've tried:
Since the 'constant' pragma will accept a hash reference, I thought I
could define a hash, pass it's reference to constant and it's keys to
EXPORT_OK:
my %build_properties = (
CURRENTCHANGE => 'currentChange',
BLDNUM => 'buildNumber',
STAGEROOTDIR => 'stageRootDir',
WEBREPORTDIR => 'webReportDir',
);
use constant \%build_properties;
@EXPORT_OK = (keys %build_properties);
That produces this error from the calling script:
Undefined subroutine &Constants::CURRENTCHANGE called ...
Changing the export to this:
@EXPORT_OK = qw(CURRENTCHANGE BLDNUM WEBREPORTDIR STAGEROOTDIR);
produced the same error.
The conventional way
@EXPORT_OK = qw(CURRENTCHANGE BLDNUM WEBREPORTDIR STAGEROOTDIR);
use constant {
CURRENTCHANGE => 'currentChange',
BLDNUM => 'buildNumber',
STAGEROOTDIR => 'stageRootDir',
WEBREPORTDIR => 'webReportDir',
};
works, so all seems fine with the calling script. The problem *seems*
to be that
use constant \%build_properties
is not having the desired effect.
So, my questions are
1. Is there something wrong with 'use constant \%hash' ?
2. Is what I'm attempting to do bad Perl style?
3. If not 2, is there a preferred way to do this?
Thanks.
shared among several Perl scripts. I'm looking for a way to simplify
getting all the constant names into the @EXPORT_OK array, so I don't
end up with this:
@EXPORT_OK = qw(CURRENTCHANGE BLDNUM STAGEROOTDIR ...);
because I can envision that list becoming large, and everytime I
define a new constant I have to remember to add it to the list.
Here's what I've tried:
Since the 'constant' pragma will accept a hash reference, I thought I
could define a hash, pass it's reference to constant and it's keys to
EXPORT_OK:
my %build_properties = (
CURRENTCHANGE => 'currentChange',
BLDNUM => 'buildNumber',
STAGEROOTDIR => 'stageRootDir',
WEBREPORTDIR => 'webReportDir',
);
use constant \%build_properties;
@EXPORT_OK = (keys %build_properties);
That produces this error from the calling script:
Undefined subroutine &Constants::CURRENTCHANGE called ...
Changing the export to this:
@EXPORT_OK = qw(CURRENTCHANGE BLDNUM WEBREPORTDIR STAGEROOTDIR);
produced the same error.
The conventional way
@EXPORT_OK = qw(CURRENTCHANGE BLDNUM WEBREPORTDIR STAGEROOTDIR);
use constant {
CURRENTCHANGE => 'currentChange',
BLDNUM => 'buildNumber',
STAGEROOTDIR => 'stageRootDir',
WEBREPORTDIR => 'webReportDir',
};
works, so all seems fine with the calling script. The problem *seems*
to be that
use constant \%build_properties
is not having the desired effect.
So, my questions are
1. Is there something wrong with 'use constant \%hash' ?
2. Is what I'm attempting to do bad Perl style?
3. If not 2, is there a preferred way to do this?
Thanks.