perl use vars pragma使用技巧

perl 中的vars是perl中的一个pragma(预编译指示符),专门用来预定义全局变量,这些预定义后的全局变量在qw()列表中,在整个引用perl文件中皆可使用,即便使用use strict也不会报错:


use strict ;
$str = "hello world!n" ;

报错信息:Global symbol "$str" requires explicit package name at ~vars.pl line 3.
Execution of ~vars.pl aborted due to complication errors.

引用use vars后执行结果:


use strict ;
use vars qw($str) ;
$str = "hello world!n" ;
print $str ;

Output :

hello world!