in reply to Re^5: Which "use" statements in a module do "bubble up" to the callers?
in thread Which "use" statements in a module do "bubble up" to the callers? [SOLVED]

Wow, that is tricky. Thank you very much!

I would not have come to the idea that a pragma's subpragma might be scoped in another way than the pragma itself or its other subpragmas. That's the reason why I haven't found it in the documentation. But even if I had found it: IMHO, "... without mentioning lexical scope." for most people (including myself) implies "With this subpragma, nothing is different regarding the lexical scope from what has been said earlier in that text". This is very worrying and probably should be improved (to make myself clear: The Perl documentation is one of the best documentations I have ever seen, but even in the best documentation, there is always room for improvement).

Finally, thinking about your last statement in your last comment took me quite a while, and I did some tests. If choroba really tried to say what you suspect, I think he is wrong:

If I have a lexically scoped pragma at the top my main script, and if I then use another module, the pragma will not be in effect during initialization (i.e. during loading and compiling at compile time) of that other module. I can give a code example if I didn't manage to express clearly enough what I wanted to say.

Thank you very much,

Nocturnus

  • Comment on Re^6: Which "use" statements in a module do "bubble up" to the callers?
  • Download Code

Replies are listed 'Best First'.
Re^7: Which "use" statements in a module do "bubble up" to the callers? (updated)
by haukex (Archbishop) on Sep 02, 2017 at 10:56 UTC
    I would not have come to the idea that a pragma's subpragma might be scoped in another way than the pragma itself or its other subpragmas.

    I agree the documentation could be clarified. A bit of historical perspective helps a little: In Perl things generally default(ed) to being global. For example, just like the open pragma, lexically-scoped warnings were introduced in 5.6.0 (before that it was just -w and $^W) - that may be 17 years ago now, but consider that was six years after the release of Perl 5 and nine years after Perl 4. And a lot of the wordings in the documentation haven't really changed over time (including open: this is from the first implementation in March 2000). So keeping in mind the default is "global", when you look at the documentation, it's fairly explicit about which part of use open is lexically scoped.

    If I have a lexically scoped pragma at the top my main script, and if I then use another module, the pragma will not be in effect during initialization (i.e. loading and compiling at compile time) of that other module.

    Yes, that's a good point! Update: Thinking about this a bit more, some more can be said about the particular current case. Your statement is correct for pragmas like warnings and the lexically-scoped part of the open pragma. But we're talking about STD(IN|OUT|ERR) here, these handles are global and normally stay opened during the entire run of the program, unless they are explicitly changed. That means you'd have to execute code every time the lexical scope changes, but the only mechanism built in to Perl to support lexical scoping of pragmas, $^H and %^H (perlpragma), doesn't really support that directly (although it can be hacked in). So if you wanted to affect those filehandles with an effect in the lexical scope, I think you'd have to get pretty creative with modules like B::Hooks::EndOfScope. So it's not really too surprising that the open pragma isn't that advanced (although doing it for the current dynamic scope would probably be easier). Plus, what would be the use case of switching the handles's encodings mid-run? The terminal isn't going to change encodings all of a sudden, and most likely neither will files piped to and from Perl via the shell. So taking all that into account, it's not really surprising that the :std option is implemented to just globally change the handles.

      Thank you very much again, and of course you are right.

      But to clarify my last comment: I intended it to exclusively relate to your statement in your next to last comment which begins with: Even if the :std option of the open pragma were to be lexically scoped, ... This statement has made me think about it some time. Notably, if choroba had been correct, this would mean that we theoretically could remove every use pragma (lexically scoped or not) from any module we are including provided we have that pragma at the top of the main script. Obviously, this is not true.

      This has been an understanding problem for me, so I have written the comment which (as mentioned above) was only relating to lexically scoped pragmas.

      Thank you very much again,

      Nocturnus