tags:

views:

611

answers:

6

I have perl, v5.6.1 built for MSWin32-x86-multi-thread Binary build 638 provided by ActiveState.

I am working on a Perl script where I have declared constants that are used later for comparison purposes. For some reason, I am getting an error that states something along the line of Constant name has invalid characters at script's line 31 (The line right after the use constant clause in the code below). I checked and found out that '_' (underscores) in Constant name is a legit character. I also tried to change '0.00' to just '0' to see if that was the cause but I got the same error. I am not sure what I am doing wrong. Anyone know why the compiler does not like this?

Thanks!

Here is the Code:

use constant {
MIN_NET_DLR => 0.00,
MAX_NET_DLR => 99.99,
MIN_SUM_DLR => 0.00,
MAX_SUM_DLR => 999.99,
MIN_UNITS => 0,
MAX_UNITS => 99,
MIN_SUM_UNITS => 0,
MAX_SUM_UNITS => 999,
PCT_THRES_AO => 1,
PCT_THRES_TRANS_CUST_BI => 20,
PCT_THRES_CUST => 3,
};

PROBLEM:

The problem is that the version of constant provided by perl 5.6.1 does not support hash reference.

SOLUTION:

Use the regular declaration for constants. Hence, the declaration will look as follows:

use constant MIN_NET_DLR => 0.00;
use constant MAX_NET_DLR => 99.99;
use constant MIN_SUM_DLR => 0.00;
use constant MAX_SUM_DLR => 999.99;
use constant MIN_UNITS => 0;
use constant MAX_UNITS => 99;
use constant MIN_SUM_UNITS => 0;
use constant MAX_SUM_UNITS => 999;
use constant PCT_THRES_AO =>    1;
use constant PCT_THRES_TRANS_CUST_BI => 20;
use constant PCT_THRES_CUST => 3;

Thanks @leon for the solution as well as others who chimed in with their inputs.

UPDATE: Another (more elegant) solution is to update your Perl version to the one that supports hash reference in declaring constants.

A: 

Works for me. Are you sure you don't have any control characters in that code somewhere?

zigdon
I moved just the constant declaration portion to a test script and ran that and I still get the error. Does it matter that I am running ActivePerl in Windows XP? If it is running for you, it might be an environment thing for me.
tundal45
Possible - I was testing on linux.
zigdon
A: 

Remove the last comma at the end of this line:

PCT_THRES_CUST => 3,

That could be the issue.

Bill Turner
Comma's have always been allowed there.
Leon Timmermans
Perl allows trailing commas in lists.
brian d foy
+11  A: 

The problem is that you are using a hash reference. The version of constant provided by perl 5.6.1 does not support that. You'll either have to remove the braces or upgrade your constant.

Leon Timmermans
Thank you Leon and thanks everyone who jumped in an helped. The problem was that I was using hash reference. Once I declared each constant with use constant in front of it, it went away. A better solution of course would be to upgrade but I got few scripts that are running so have to wait. Thanks!
tundal45
...Or upgrade your Perl!
brian d foy
...or use parentheses to create a real hash instead of a hashref.
R. Bemrose
+2  A: 

It works fine for me on perl5.8 but doesn't work on perl5.6 . The error message is:

Can't define "HASH(0x80c05d8)" as constant (name contains invalid characters or is empty)

Seems like an old-version issue, probably the issue that Leon described.

Igor Oks
+1  A: 

Your old perl probably does not support the syntax where you declare multiple constants in one statement. Try defining each constant separately like this...

use constant MAX_NET_DLR => 99.99;
noswonky
@noswonky - Thanks. I tried that yesterday and it worked. I will update the question with the answer.
tundal45
+1  A: 

I would use the Readonly module.

The disadvantage of constant is, that it creates a function within the callers namespace which returns a constant value.

Readonly can be used to declare scalars, arrays and hashes readonly (real constants).

Jens
It is also important to note the performance hit for hashes and arrays (scalars are fine if you have Readonly::XS) when using Readonly. Also, constants created by constant can be resolved during compile time in expressions like "sleep(60*WAIT_MINUTES);" or "debug($str) if DEBUG;"
Chas. Owens