943,936 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 5404
  • Perl RSS
Feb 1st, 2007
0

Making global constants/Making constants global

Expand Post »
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.
Similar Threads
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Feb 6th, 2007
0

Re: Making global constants/Making constants global

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
Reputation Points: 18
Solved Threads: 6
Junior Poster in Training
lordspace is offline Offline
90 posts
since May 2006
Feb 6th, 2007
0

Re: Making global constants/Making constants global

Quote ...
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:

Quote ...
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.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Feb 7th, 2007
0

Re: Making global constants/Making constants global

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();
Reputation Points: 18
Solved Threads: 6
Junior Poster in Training
lordspace is offline Offline
90 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Someone PLEASE help end the loops!!!
Next Thread in Perl Forum Timeline: Creating a Robust Logout System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC