http://www.perlmonks.org?node_id=1021939

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

How can I system wide include a directory in @INC so ANY use of perl always has it in @INC?

I am on Ubuntu 12.04 if it matters. I don't want to use enviroment varibles because that are not always passed through to everyones shells.

At the moment I have to push to @INC inside a 'begin' for every file...

Thanks, Mike

Replies are listed 'Best First'.
Re: Add directry to @INC system wide
by ikegami (Patriarch) on Mar 06, 2013 at 04:43 UTC

    @INC is built when perl is built, so you'll have to rebuild perl with the extra paths specified in the Configure stage.

    Why didn't you install the module in the expected directory if you have the power to affect all users?

Re: Add directry to @INC system wide
by kielstirling (Scribe) on Mar 06, 2013 at 05:00 UTC
    I add the following to my programs
    use FindBin qw($Bin); use lib "$Bin/lib";

    Then you can use any module(s) located in the ./lib directory.
    -Kiel
      Using $RealBin instead of $Bin means your program won't break if you create a symlink to the script.
Re: Add directry to @INC system wide
by vinoth.ree (Monsignor) on Mar 06, 2013 at 05:33 UTC
    kielstirling ++

    I am also using the same method, here it is,

    use File::Spec::Functions qw(catdir splitdir canonpath); use FindBin qw($RealBin); BEGIN { my @dirs = splitdir(canonpath($RealBin)); my($path) = grep { -e $_ } ( map { $_ = catdir(@dirs[0 .. $#dirs - + $_]).'/bin/new/' } (0 .. $#dirs) ); unshift(@INC, $RealBin, $path); }
Re: Add directry to @INC system wide
by myforwik (Acolyte) on Mar 06, 2013 at 05:30 UTC

    It looks like the simplist way is to symlink all the modules into a directory already in @INC

Re: Add directry to @INC system wide
by Anonymous Monk on Mar 06, 2013 at 07:40 UTC

    How can I system wide include a directory in @INC so ANY use of perl always has it in @INC?

    Recompile perl, and configure otherlibdirs option

Re: Add directry to @INC system wide
by Anonymous Monk on Mar 06, 2013 at 06:04 UTC
    Just set the PERL5LIB environment variable to the directory (or directories) you wish to include, and they will be prepended to @INC for every perl.

    Cheers,
    Rob
      From the OP:
      I don't want to use enviroment varibles because that are not always passed through to everyones shells.
Re: Add directry to @INC system wide
by Perlbotics (Archbishop) on Mar 07, 2013 at 21:14 UTC

    Maybe, you can edit the system wide perl customization file?
    Run the following one liner to see if your Perl installation was compiled supporting this hook:

    perl -MConfig -e 'print $Config{usesitecustomize} ? "yes, edit $Confi +g{sitelib}/sitecustomize.pl" : "no luck", "\n"'

    Then, in ..../sitecustomize.pl:

    use lib qw(/my/preferred/dir); print "SITECUSTOMIZE: INC: @INC\n" if $ENV{DEBUG_SITECUSTOMIZE}; # j +ust for fun

    However, users can prevent loading this file using the -f switch (see perlrun). If this is not acceptable, you need to compile your own perl executable as other monks already suggested.

      Maybe, you can edit the system wide perl customization file?

      That can be disabled -- meaning not always passed through -- the only way to permanently add paths which cannot be disabled is to compile-them-in

      OTOH, simply installing modules in @INC to begin-with avoids the problem of needing to add paths to @INC

        Just in case anyone else should follow this node;
        It occurs to me that one could also create a module that "pre-pends" the desired
        additional directory the system' include path, which could then become a system
        wide include, by creating a wrapper for the Perl executable.
        If the module name were "prepend-lib-path.pm" the "wrapper" that disguises itself as
        Perl proper, would contain:
        use prepend-lib-path;
        While not an excuse to omit "lib" paths you might later decide you need;
        it might help in a pinch.

        HTH

        #!/usr/bin/perl -Tw
        use perl::always;
        my $perl_version = "5.12.4";
        print $perl_version;