如果我不使用严格;以下代码工作正常并打印出“alice”:
assign_name(); print_name(); sub assign_name { $name = "alice"; } sub print_name { print $name; }
但是当我使用严格;然后我知道我必须在使用之前声明变量.我读到某个地方我应该使用我们而不是我来声明一个全局变量.所以我有以下几点:
use strict; use warnings; assign_name(); print_name(); sub assign_name { our $name = "alice"; } sub print_name { print $name; # This is line 12. }
然后我收到以下错误:
Variable "$name" is not imported at test.pl line 12. Global symbol "$name" requires explicit package name at test.pl line 12. Execution of test.pl aborted due to compilation errors.
请帮忙.
解决方法
只需声明变量,两个潜艇都可以看到它.
use strict; use warnings; my $name; assign_name(); print_name(); sub assign_name { $name = "alice"; } sub print_name { print $name; }
(我没有理由在这里使用!)