develooper Front page | perl.perl5.porters | Postings from March 2006

how should %^H work with lexical pramas

Thread Next
From:
Nicholas Clark
Date:
March 28, 2006 10:57
Subject:
how should %^H work with lexical pramas
Message ID:
20060328185710.GG48367@plum.flirble.org
Thanks to Robin's insight about passing %^H to eval, lexical pragmata work
almost perfectly. As I understand it, the only thing missing is that code
can't tell at runtime what the relevant compile time state of lexical pragmata
was. So code in the BEGIN block at 'b' can see the effect of use sort:

$ ./perl -Ilib -lwe 'BEGIN {print "a: " . keys(%^H)}; use sort "stable"; BEGIN {print "b: " . keys(%^H)} print "c: " . keys(%^H)'
a: 0
b: 1
c: 0


but regular code at 'c' can't.

Logically, it seems, the way to go is to add something to make accesses of %^H
at run time report the state of the compile time lexical pragmata. %^H should
therefore be read only at runtime.

Except that this doesn't work. One scope's compile time is another scope's
runtime. sort.pm's implementation uses read/write access to %^H to affect
how its caller's subsequent compilation behaves:

        if (/^_q(?:uick)?sort$/) {
            $^H{sort} &= ~$sort::sort_bits;
            $^H{sort} |=  $sort::quicksort_bit;
        } elsif ($_ eq '_mergesort') {
            $^H{sort} &= ~$sort::sort_bits;
            $^H{sort} |=  $sort::mergesort_bit;
        } elsif ($_ eq 'stable') {
            $^H{sort} |=  $sort::stable_bit;
        } elsif ($_ eq 'defaults') {
            $^H{sort} =   0;
        } else {


So how should all this stuff work? I think there are actually two hashes, or
things-with-a-hash-interface that need to exist. A read only thing that
reflects the hints in force at the time of compilation of this statement.
(This can be slow, but probably needs to be memory efficient). And a
read/write thing which reflects/affects the compilation of the immediate
lexical caller, if they are in compile time and we're in run time.

Is this sane? If so, what are they called? And how best does the "read only"
thing track any updates to the state of the read/write thing? And store them
efficiently?

Nicholas Clark


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About