http://qs321.pair.com?node_id=11141617

perlancar has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to do no lib from the command-line option (the opposite of -I or -Mlib=...)? I want to remove entries from @INC, or to be more exact I want to set @INC to an exactt list of directories. Preferably without the use of -e or other non-core modules.

Replies are listed 'Best First'.
Re: Removing entries from @INC
by Fletch (Bishop) on Feb 24, 2022 at 14:56 UTC

    As documented in perlrun if the first character of the library name after -M / -m is a dash the use is treated as no instead: perl -M-lib=/TO/REMOVE ...

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thanks! Now that you pointed it out, I vaguely remember reading about this years ago.
Re: Removing entries from @INC
by ikegami (Patriarch) on Feb 24, 2022 at 19:33 UTC

    I want to set @INC to an exactly list of directories

    eh? You've surely done something wrong along the way if you need to do this.

    Why do you think you need to remove entries from @INC?

      I have a perl script that starts another instance of perl interpreter, and I want the latter to inherit the original script's @INC.

      The actual case is I'm testing (prove -l) a script in a Perl distribution which starts another perl script that requires a module in the current distribution. The latter script would fail to find the module because its @INC does not contain lib. Now in this specific case I can just add -Ilib option to the latter script's invocation when detecting some environment variable, but I'm curious about the more generic solution.

        Have script #1 add to PERL5LIB whatever it adds to @INC.

Re: Removing entries from @INC
by LanX (Saint) on Feb 24, 2022 at 14:44 UTC
    did you try ... ?
    BEGIN { @INC = YOUR_LIST; }

    ... on top of your script?

    update

    > from the command-line option

    Oops, I missed that, sorry.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      printf ' package cleanlib; sub import { @INC = ( ); } 1 ' >cleanlib.pm perl -Mlib=. -Mcleanlib -Mlib=... ...