develooper Front page | perl.perl5.porters | Postings from June 2017

Re: lib/_charnames.pm puts references in %^H

Thread Previous | Thread Next
From:
demerphq
Date:
June 15, 2017 00:35
Subject:
Re: lib/_charnames.pm puts references in %^H
Message ID:
CANgJU+WTZpbwm-9XuA3JYVtTrB=j0ShEwopKvY3TT_PO-J594g@mail.gmail.com
On 15 June 2017 at 01:33, Zefram <zefram@fysh.org> wrote:
> demerphq wrote:
>>But the value that was set at compile time does not appear to be set
>>at run time.
>
> Yes, that's correct.  %^H's scoping behaviour is driven by the scoping of
> code being compiled, not the scoping of code being run.  That's because
> the value of %^H is lexical state that's in effect for the code being
> compiled, not for the code being run.  When you're not compiling anything,
> %^H behaves essentially as an ordinary hash.
>
> Since you didn't assign anything to $^H{t} during runtime of your test
> program, of course it doesn't contain anything at runtime.  If you want
> to see at runtime the $^H{t} value from compilation of the running code
> (i.e., the lexical hint applying to the running code), you need to define
> something like
>
>         sub ht { ((caller(0))[10] // {})->{t} }
>
> and print out its return value.
>
>>I poked into this further and it seems to be true also fro $^H,
>
> Same scoping behaviour.  caller item [8] to see at runtime the value
> that it had at compile time of the running code.
>
>>$ perl -le'use charnames (); use bytes (); sub translator {if ($^H &
>>$bytes::hints_bits) { print "bytes" } else { print "unicode" } return
>>"test" } BEGIN { $^H{charnames}= \&translator}; { use bytes; print
>>"\N{TEST}" } '
>
> $bytes::hints_bits is undefined.  You want $bytes::hint_bits, and with
> that alteration this does print "bytes".

I see. And that only works because translator() is being called at
compile time. Would it be fair to say that it is as though the sub
being called by "\N{TEST}" was wrapped in a BEGIN?

BTW, I feel like the docs for $^H and %^H do not explain what you have
explained in this thread at all. I think i will make a stab at a doc
patch based on your words.

Yves


-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | 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