Making global constants/Making constants global

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Making global constants/Making constants global

 
0
  #1
Feb 1st, 2007
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.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 66
Reputation: lordspace is an unknown quantity at this point 
Solved Threads: 4
lordspace's Avatar
lordspace lordspace is offline Offline
Junior Poster in Training

Re: Making global constants/Making constants global

 
0
  #2
Feb 6th, 2007
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
Last edited by lordspace; Feb 6th, 2007 at 12:08 pm. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Making global constants/Making constants global

 
0
  #3
Feb 6th, 2007
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.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 66
Reputation: lordspace is an unknown quantity at this point 
Solved Threads: 4
lordspace's Avatar
lordspace lordspace is offline Offline
Junior Poster in Training

Re: Making global constants/Making constants global

 
0
  #4
Feb 7th, 2007
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();
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC