如果要设定一个模块的搜索路径,又不想在“~/.bashrc”中去永久性的添加一行 “PERL5LIB”。那可以在脚本中直接修改@INC;
#!/usr/bin/perl -w
use strict;
use File::Basename;
use Cwd;
use lib '/mnt/lustre/user/wubin/01.Program/Scripts/01.script/GeneLab';
use Common;
my $common = bless {}, 'Common';
=pod
这样写是错的,因为perl脚本在运行之前已经加载了@INC。
要想临时改变@INC,必须在BEGIN代码块里面修改。
my $script_path = Cwd::abs_path($0);
my $package_bin = dirname($script_path);
unshift(@INC, $package_bin);
=cut
BEGIN{
our @INC; #这非常重要,没有就行不通
my $script_path = Cwd::abs_path($0);
my $package_bin = dirname($script_path);
unshift(@INC, $package_bin); #https://codeverge.com/perl.beginners/empty-compile-time-value-given-to-use-lib/139946
}