这个Perl代码有什么作用?

前端之家收集整理的这篇文章主要介绍了这个Perl代码有什么作用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在cPanel中,它们告诉您将此代码插入Perl文件的开头.我不确定它是做什么的.我已经在文件的开头尝试了有和没有这个的代码,似乎所有工作都是一样的.我没有用cron运行代码来测试它,但只有我自己.通过“测试它”,我的意思是使用打印线,数据库连接和返回,潜艇,变量等……

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

解决方法

也许更容易阅读:

# The BEGIN block is explained in perldoc perlmod

BEGIN {
    # Prefix all dirs already in the include path,with root's perl path if it exists,or the
    # current user's 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's 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); 
}

猜你在找的Perl相关文章