What does this Perl code do?

In cPanel, they tell you to insert this code at the top of the Perl files. I'm not sure what he is doing. I tried the code with and without this at the beginning of the file, and everything seems to work the same. I have not tested this with cron managing code, but just like me. "Checking this out", I mean using print strings, database connections and returns, subtypes, vars, etc.

BEGIN 
{
    my $base_module_dir = (-d '/home/root/perl' ? '/home/root/perl' : ( getpwuid($>) )[7] . '/perl/');
    unshift @INC, map { $base_module_dir . $_ } @INC;
}
+3
source share
3 answers

Maybe a little easier to read:

# The BEGIN block is explained in perldoc perlmod

BEGIN {
    # Prefix all dirs already in the include path, with root perl path if it exists, or the
    # current user perl path if not and make perl look for modules in those paths first:
    # Example: 
    #     "/usr/lib/perl" => "/home/root/perl/usr/lib/perl, /usr/lib/perl"

    my $root_user_perl_dir = '/home/root/perl';

    # Fetch user home dir in a non-intuitive way:
    # my $user_perl_dir = ( getpwuid($>) )[7] . '/perl/');

    # Fetch user home dir slightly more intuitive:
    my $current_userid        = $>; # EFFECTIVE_USER_ID see perldoc perlvar
    # See perldoc perlfunc / perldoc -f getpwuid
    my ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell,$expire) 
        = getpwuid($current_userid); 
    my $current_user_home_dir = $dir; 
    my $user_perl_dir         = $current_user_home_dir . '/perl/';

    my $base_module_dir = '';

    if (-d $root_user_perl_dir ) { 
        # Use this if the path exists
        $base_module_dir = $root_user_perl_dir;
    }
    else { 
        # or fallback to current user path
        $base_module_dir = $user_perl_dir;
    }

    # Generate the new paths
    my @prefixed_INC = map { $base_module_dir . $_ } @INC;

    # Add the generated paths in front of the existing ones.
    @INC = (@prefixed_INC, @INC); 
}
+8
source

. , ( ) perl/ . , @INC. @INC. , , CPanel, , ( cgi) .

BEGIN , , - .

, /home/root/perl . , $base_module_dir, <user home>/perl/. , perl , .

getpwuid($>). getpwuid() ( , passwd Unix) . $> - script. 7 , ( 8- passwd, ).

@INC $base_module_dir @INC. $base_module_dir , @INC. map .

+10

Perl /home/root/perl - - ~/perl . , Perl, .

, , Perl .

BEGIN, , , @INC, use.

+3
source

Source: https://habr.com/ru/post/1761536/


All Articles