Countering Constants in Unit Tests

Feb 16, 2012 12:30

In Perl, some people will do funky stuff like this:

package FooBar;
use constant {
CONFIG_VALUE_A = 'foo',
CONFIG_VALUE_B = 'bar',
};

If I want to override those values (e.g.: while running your application on my system, or building a test suite) I have to use a construct like this:

*{FOOBAR::CONFIG_VALUE_A} = sub { 'my new foo' };
Please don't use constants for things which might vary (such as hostname, database server, database name, user name or password). Constants are for things like Pi, or G or h (and even then, the cosmologists tell us that h is not really constant in space or time).

To handle configuration variables, I recommend tools such as Config::IniFiles.

That is all.

perl, tdd, engineers-log, geek, programming

Previous post Next post
Up