我知道如何在Perl中从一个包中使用变量到另一个包中。我正在尝试在另一个Perl脚本test2.pl
中使用在test1.pl
声明的全局变量。我使用require
来加载perl文件。
#!usr/bin/perl #test1.pl
use strict;
use warnings;
out $db_name; #global variable to be used in another script
fun();
sub fun
{
$db_name = 'xxyy';
}
#!usr/bin/perl #test2.pl
require 'test1.pl'; #require is used to include the perl script like we use "use" for importing packages
my $database = $db_name; #global variable from previous script
use strict;
use warnings;
testing();
sub testing
{
print "$database\n";
}