954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Making global constants/Making constants global

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.

MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
 

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

lordspace
Junior Poster in Training
90 posts since May 2006
Reputation Points: 18
Solved Threads: 6
 

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.

MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
 

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();

lordspace
Junior Poster in Training
90 posts since May 2006
Reputation Points: 18
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You