in reply to A lexical lib pragma?

Consider the following:

sub foo { say "Pragma out of scope:"; say for @INC; } { use lib::lexical 'path'; say "Pragma in scope:"; say for @INC; foo(); }

If the pragma modifies @INC and resets it at end of scope, foo will see the change. A hook is by far the easiest solution. Otherwise, you have to convince the parser to handle use, require and do differently than normal.

do and require can be done using an opcode checker, but use can't be done that way because of its compile-time effect. Maybe a call_parser/call_checker would work?

Anyway, I don't see the point of such a pragma. One a module is loaded, it stays loaded regardless of any changes to @INC, so the effect of changing @INC is global even if the change is temporary.

Replies are listed 'Best First'.
Re^2: A lexical lib pragma?
by perlancar (Hermit) on Jan 04, 2020 at 08:01 UTC

    Your example does show that the effect of "use lib::lexical" is actually, just like local, dynamic instead of lexical. I wouldn't say "global" as the @INC is indeed is restored at the end of the scope. The secondary effect (or tertiary and so on) can be global and/or irreversible but this is brought upon by other action (do/require/use) and not by "use lib::lexical" or the change to @INC itself. And that can be said about any lexical pragma.

    I did originally imagine lib::lexical mainly as a syntax sugar for 'local @INC = (..., @INC)'.

      The change is seen everywhere in the interpreter, and that's the very definition of global. In any case, it's definitely not lexical! The point is that changes to @INC could have very far reaching effects. The fact that the change is temporary doesn't change that.

      I did originally imagine lib::lexical mainly as a syntax sugar for 'local @INC = (..., @INC)'.

      That would be FAR clearer to the reader! (But just as dangerous and pointless.)