needperlhelp has asked for the wisdom of the Perl Monks concerning the following question:

How do i create a constant from a scalar
$test = 'value'; use constant CONST_VAL1 => $ENV{'PATH'}; # This works use constant CONST_VAL2 => scalar localtime # This works use constant CONST_VAL3 => $test ; # This doesn't work
upon printing CONST_VAL1 , CONST_VAL2 , the values are printed,
but for CONST_VAL3 i get a empty string.
any clues on how to handle this situation !!!!, thanks

2006-09-05 Retitled by holli, as per Monastery guidelines
Original title: 'constants :-('

Replies are listed 'Best First'.
Re: problem with creating constants from scalar
by Fletch (Bishop) on Aug 30, 2006 at 03:38 UTC

    use happens at compile time. Your $test hasn't been assigned a value yet. Wrap it in a BEGIN { } block and it will work.

      Perfect , many thanks for the help
Re: problem with creating constants from scalar
by duff (Parson) on Aug 30, 2006 at 03:48 UTC

    Also, you might want to consider using the Readonly module.