Che*_*eso 3 perl include cpanel
在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;
}
Run Code Online (Sandbox Code Playgroud)
Her*_*rbN 10
它旨在设置您的模块搜索路径.具体来说,它将默认位置(已检查的第一个位置)设置为用户的本地perl/目录.它不仅添加了该目录,还使其成为新的目录@INC. 它为@INC中的每个条目执行此操作. 在有限的访问环境中,例如那些使用CPanel的环境,这可以保证您的脚本(通用cgi)可以使用您的模块而不是其他任何模块.
BEGIN表示它发生在不在块中的任何代码之前.
第一行确定是否/home/root/perl存在并且是目录.如果两者都为真,则将其分配给$base_module_dir,否则分配<user home>/perl/给变量.请记住,在perl中,如果函数调用返回列表,则可以直接对其进行索引.
它找到用户的主目录getpwuid($>).getpwuid()获取给定用户的用户帐户信息(通常来自Unix系统上的passwd)并将其作为列表返回.$>是脚本的有效用户ID.索引为7的原因是列表中主目录的位置(如果内存服务,它是passwd中的第8个字段).
然后它会在所有条目中添加@INC,$base_module_dir并在前面插入修改后的条目@INC.因此,它不只是$base_module_dir作为目录添加,而是将其添加为所有条目的新根目录@INC.这就是为什么它使用map而不是只添加一个条目.
也许更容易阅读:
# 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
464 次 |
| 最近记录: |