我试图在另一个包中设置全局变量,但不幸的是,它被设置为undef,尽管它在调试模式中是正确的显示参数变量,但将其设置为undef
这是我的密码。
SuperFunc.pm
#!/usr/bin/perl
use strict;
use warnings;
package SuperFunc;
my $global_x = 20;
my $global_y = 20;
sub set_x {
my $new_x = shift;
if($new_x) {
$global_x = $new_x;
}
}
sub set_y {
my $new_y = shift;
if($new_y) {
$global_y = $new_y;
}
}
和Main.pl
.......
SuperFunc::set_x($x);
SuperFunc::set_y($y);
.......
怎么了?
它似乎运行良好。如果没有更多的代码,我真的无法理解这个问题。
也许你在寻找http://perldoc.perl.org/functions/our.html?
编写脚本
use strict;
use lib "./lib";
use SuperFunc;
SuperFunc::log();
SuperFunc::set_x(1);
SuperFunc::set_y(2);
SuperFunc::log();
模块
#!/usr/bin/perl
use strict;
use warnings;
package SuperFunc;
my $global_x = 20;
my $global_y = 20;
sub set_x {
my $new_x = shift;
if($new_x) {
$global_x = $new_x;
}
}
sub set_y {
my $new_y = shift;
if($new_y) {
$global_y = $new_y;
}
}
sub log {
print "$global_xn$global_y";
}
输出
20
20
1
2