Is there a standard way to make constants defined using the use constant pragma available to all of the packages that are use'd from an executed file?

Or even better, to make the constants defined in a package available to other packages that use that package? It seems like anything defined using use constant is only available in the scope of a single package (and is even unavailable to OO packages that use @ISA to inherit another package).

That means having to declare certain constants multiple times. Which kind of defeats the purpose of these constants.

Recommended Answers

All 3 Replies

You may would like to try

our $MY_VARIABLE_AS_CONSTANT;

this variable should be available in all the packages.

the other option is to have a package that exports functions in main:: namespace.

Svet

You may would like to try

our $MY_VARIABLE_AS_CONSTANT;

That's pretty much what I ended up doing; I like the 'keyword' appearance of constants though.

like STATUS_ERROR instead of $STATUS_ERROR :lol:

the other option is to have a package that exports functions in main:: namespace.

I tried something similar; to have a package that returns the STATUS_ERROR constant from a get_error_status() function, and check it with an is_error_status($) function.

That got hella convuluted after a while.

if you use a package and not an object from the class you should write SomePackage::method ...

you may also have a package that acts as a container

e.g.

Package Status

$ref = Status::getInstance();
Status::getLastError();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.