perl/mason: assigning value to a global variable in handler.pl

575 Views Asked by At

Is it possible to assign value to a global variable in handler.pl?

I need to assign a value to a global variable in handler.pl, and get the value from a Mason component.

I tried this way:

httpd.conf

...
PerlRequire handler.pl
...

handler.pl

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

something.mas

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

but it is not working, it doesn't return <h1>test</h1> but just <h1></h1> as $x is undefined. How can I make it work?

1

There are 1 best solutions below

0
On

Yes, but you'll have to set

PerlSetVar MasonAllowGlobals $x 

in httpd.conf, or in handler.pl include

allow_globals => [ '$x' ]

in your apache handler definition, or declare it in the HTML::Mason::Commands package that the components run in:

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

This last option is also how you can make other Perl modules available in all components:

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

Refer to http://www.masonhq.com/?FAQ:Components#h-can_i_use_globals_in_components_