Tree
Tree

Reputation: 10362

How to change a constant's value in perl

our $TEST;
*TEST = \100;
$TEST =200

I want to change TEST's value to 200 for specific reasons. Is it possible to change it?

Upvotes: 0

Views: 914

Answers (4)

Evan Carroll
Evan Carroll

Reputation: 1

This isn't the write way to do a constant in perl. The right way is

use constant TEST => 100;

Now if that's declared in a module, and you want to change that value from a test you can put

use unconstant;

At the top, and then you can follow that up with another declaration.

use unconstant;
use MyLib;
use constant *MyLib::TEST => 200;

I am the author of unconstant

Upvotes: 0

Saurabh Hirani
Saurabh Hirani

Reputation: 1248

If you do use constansts and then use a constant value, then before messing around with it remember that Perl normally optimizes away constant code - i.e. maybe skipping an if condition which said - if (DEBUG) or hard coding a value (e.g. PI) during its intermediate byte code generation. constant pragma

Upvotes: 0

shawnhcorey
shawnhcorey

Reputation: 3601

Try adding this statement:

local $TEST = 200;

Upvotes: 2

Eugene Yarmash
Eugene Yarmash

Reputation: 150128

You can use the same syntax : *TEST = \200
BTW, you may want to look at Const::Fast.

Upvotes: 2

Related Questions