【问题标题】:perl/mason: assigning value to a global variable in handler.plperl/mason:为 handler.pl 中的全局变量赋值
【发布时间】:2011-11-06 13:26:57
【问题描述】:

是否可以为handler.pl中的全局变量赋值?

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

我试过这样:

httpd.conf

...
PerlRequire handler.pl
...

handler.pl

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

something.mas

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

但它不起作用,它不返回&lt;h1&gt;test&lt;/h1&gt;,而只是返回&lt;h1&gt;&lt;/h1&gt;,因为$x 未定义。我怎样才能让它发挥作用?

【问题讨论】:

    标签: perl mason


    【解决方案1】:

    是的,但你必须设置

    PerlSetVar MasonAllowGlobals $x 
    

    在 httpd.conf 中,或在 handler.pl 中包含

    allow_globals => [ '$x' ]
    

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

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

    这最后一个选项也是使其他 Perl 模块在所有组件中可用的方法:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多