我知道如何在 Perl 中使用一个包到另一个包的变量。我正在尝试使用声明的全局变量test1.pl
在另一个 Perl 脚本中test2.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";
}
如果您创建一个“真正的”模块,这一切都会容易得多。需要这样的库是 Perl 4 的一个技巧。
在 DBName.pm 中,您有:
package DBName;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT = qw[$DBName];
our $DBName = 'xxyz';
1;
并且在调用程序中:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use DBName;
sub testing {
say $DBName;
}
testing();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)