in reply to Re^2: Using constants as hash keys
in thread Using constants as hash keys

I do hope the points Anon raised are among "topics for meditation", since much of the talk is about constant folding at the compile time. Anyhow, there's little to fault in his overall assessment.

Consider the enumerated constant in C:

enum { FOO = 2 }; enum { FOO = 3 }; // error: redefinition of enumerator
Contrast this with Perl:
*FOO = \1.2; *FOO = \3.4; print our $FOO; # prints 3.4
Perl has no means to protect an identifier in a given scope. This is useful for example in guarding against file level symbol clashes. (Perl isn't C, of course, but some perl modules do export lots of symbols: Fcntl, POSIX, ....)

Another sign of the problem is that constants are commonly used via hashes to keep things tidy and structured. A particular case would be the use Config. If you have conditional code-paths that depend on, say, $Config{longsize}, then these tests are not folded during compilation even though %Config::Config is supposedly read-only.

Replies are listed 'Best First'.
Re^4: Using constants as hash keys
by Your Mother (Archbishop) on Jan 08, 2018 at 17:02 UTC
    Perl has no means to protect an identifier in a given scope.

    This is academic to me so it's not a rhetorical question: does Readonly not qualify?

Re^4: Using constants as hash keys
by haukex (Archbishop) on Jan 09, 2018 at 19:58 UTC
    I do hope the points Anon raised are among "topics for meditation", since much of the talk is about constant folding at the compile time.

    Absolutely, there is room for meditation and discussion, and I did name some of the limitations of Perl's readonly variables. You also raise some very good points!

    Anyhow, there's little to fault in his overall assessment.

    I disagree, as I hope I made clear in my post. In particular, 'it really doesn't have the notion of a "compile-time constant,"' and 'You might well not actually know ... the behavior of the language-constructs that you are looking at' make it seem that the post is unfortunately based on misinformation or a misunderstanding - not a good basis for discussion.