2009-02-12

Adding to the Perl Include Path

[this is from the Perl Module Installer documentation supplied with cPanel and CenOS]

Suppose you need to add a path - say /home/earthsid/perl - to the Perl include path. You will need to modify the Perl code to set the INC path in the BEGIN block of the program.

You can do this by adding the following code to your script:

BEGIN {
    my $homedir = ( getpwuid($>) )[7];
    my @user_include;
    foreach my $path (@INC) {
        if ( -d $homedir . '/perl' . $path ) {
            push @user_include, $homedir . '/perl' . $path;
        }
    }
    unshift @INC, @user_include;
}

Note that - no matter where the BEGIN block resides in a program, it is always excuted first, before ay other part of hte script lib.