SoFunction
Updated on 2025-04-08

Perl use vars pragma usage tips

The vars in perl is a pragma (precompiled indicator) in perl, which is specially used to predefined global variables. These predefined global variables are available in the qw() list in the entire reference perl file. Even if you use use strict, you will not report an error:

Copy the codeThe code is as follows:

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

Error message: Global symbol "$str" requires explicit package name at ~ line 3.
Execution of ~ aborted due to complication errors.

Execution result after reference to use vars:

Copy the codeThe code is as follows:

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

Output :

hello world!