Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Using constants as hash keys

by haukex (Archbishop)
on Jan 07, 2018 at 20:01 UTC ( [id://1206863]=note: print w/replies, xml ) Need Help??


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

it really doesn't have the notion of a "compile-time constant,"

I have to say this is incorrect. Perl has Constant Functions and their effect is compile-time, note how print "foo" is optimized away completely  (edited slightly for brevity):

$ perl -MO=Deparse use constant DOFOO => 0; my $DOBAR = 0; print "foo" if DOFOO; print "bar" if $DOBAR; __END__ use constant ('DOFOO', 0); my $DOBAR = 0; '???'; print 'bar' if $DOBAR;
But, in so doing, they became misleading. You might well not actually know the implementation or the behavior of the language-constructs that you are looking at.

I find this misleading because it makes it sound like there is some undefined behavior here. Whether or not a function was inlined or not should be transparent to the user. And what choroba is writing about has more to do with the autoquoting happening on the LHS of the fat comma, which is also well defined and has nothing to do with whether it's a constant function:

use Data::Dump; use constant FOO => 123; my $x = 455; sub BAR { ++$x } my %h = ( FOO => "A", BAR => "B", (FOO) => "C", (BAR) => "D", ); dd \%h; __END__ { 123 => "C", 456 => "D", BAR => "B", FOO => "A" }
The semantics were "grafted on,"

Constant functions have been around for something like 20 years, at least as far as I can tell from a quick look at the documentation. I'm willing to concede a couple points: 1. constant, or rather readonly, variables aren't easily usable without somewhat obscure syntax (*PI=\3.14;, see Symbol Tables) or a module like Readonly, 2. readonly variables don't have the same compile-time effect, and 3. constant functions require disambiguation when used as hash keys. However, taking the post as a whole, I'm sorry but IMO it is more FUD than fact.

Replies are listed 'Best First'.
Re^3: Using constants as hash keys
by oiskuu (Hermit) on Jan 08, 2018 at 13:26 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. 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.

      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?

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found