2011-11-06 69 views
0

是否可以将值赋给全局变量handler.plperl/mason:将值赋给handler.pl中的全局变量

我需要为handler.pl中的全局变量赋值,并从Mason组件获取值。

我试着这样说:

的httpd.conf

... 
PerlRequire handler.pl 
... 

handler.pl

... 
our $x = 'test'; 
... 

something.mas

... 
<h1><% $x %></h1> 
... 

但它不工作,它不返回<h1>test</h1>但只是<h1></h1>因为$x是未定义的。我怎样才能使它工作?

回答

1

是的,但你必须设置

PerlSetVar MasonAllowGlobals $x 
在httpd.conf

,或者handler.pl包括

allow_globals => [ '$x' ] 

在Apache处理程序定义,或在HTML中声明它:: Mason :: Commands包中运行的组件:

package HTML::Mason::Commands; 
use vars '$x'; 

这最后一个选项也是如何让其他Perl模块可用ES在所有组件:

package HTML::Mason::Commands; 
use Data::Dumper; 
use URI; 
... 

参考http://www.masonhq.com/?FAQ:Components#h-can_i_use_globals_in_components_