我似乎无法创建一个对该类是全局的变量,并且可以在该类的所有子例程中使用.
我看到各种各样的例子显然有效,但我无法做任何工作.
码:
my $test = new Player(8470598);
package Player;
use strict;
use warnings;
use Const::Fast;
$Player::URL = 'asdfasdasURL';
my $test3 = '33333333';
our $test4 = '44444444444';
const $Player::test0 => 'asdfasdas000';
const my $test1 => 'asdfasdas111';
const our $test2 => 'asdfasdas222';
sub new{
print $Player::URL;
print $Player::test0;
print $test1;
print $test2;
print $test3;
print $test4;
return(bless({}, shift));
}
Run Code Online (Sandbox Code Playgroud)
输出:
Use of uninitialized value $Player::URL in print at D:\Text\Programming\Hockey\test.pl line 19.
Use of uninitialized value $Player::test0 in print at D:\Text\Programming\Hockey\test.pl line 20.
Use of uninitialized value $test1 in print at D:\Text\Programming\Hockey\test.pl line 21.
Use of uninitialized value $Player::test2 in print at D:\Text\Programming\Hockey\test.pl line 22.
Use of uninitialized value $test3 in print at D:\Text\Programming\Hockey\test.pl line 23.
Use of uninitialized value $Player::test4 in print at D:\Text\Programming\Hockey\test.pl line 24.
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
虽然整个代码将在执行之前编译,但可执行部分按顺序发生.特别是,new()调用发生在包Player中的任何赋值或const调用之前.
将所有Player代码移动到Player.pm文件并调用它将use Player;导致它在新的之前立即编译和执行并按预期工作.