in reply to Perl "Constants"

I used to use use constant; because that felt like being the "right" way. But I got annoyed by the constants not behaving like variables you don't modify. (Don't interpolate, can be autoquoted). So, I haven't used use constant; for a long time now. I use variables, whose name I usually uppercase.

Replies are listed 'Best First'.
Re^2: Perl "Constants"
by Marshall (Canon) on Jul 07, 2009 at 07:37 UTC
    "use constant" is useful for debug statements and levels. In recent versions of Perl, if the compiler can see say "if DEBUG_LEVEL3" and if knows that DEBUG_LEVEL3 is false, that statement will not even be compiled. This can result in a significant performance increase where there are lots of detailed "debug" print statements within an inner loop.

    Other than that, I think use constant is pretty worthless. I normally use a 'C' style $ALL_UPPER_CASE for constants and as a matter of programming discipline, never use something like that as a lvalue.

    I guess that was a long winded way of saying: I agree, but think about constant for debug flags when you have a lot of them or they are in performance critical sections of code.