How to define a global variable with Mason
Intro
- I tried to set up mason in my Ubuntu desktop.
- It is a very simple set up, but I still had problems, after reading parts of masonbook.com & mansonhq.com
- I hope someone can give me a hint. Please see below for more detail.
Local server
- Ubuntu 11.04 desktop
- Mason 1.44 (coming with Ubuntu)
- Newbie to perl/mason
Files
- mason.seedy2 - It is a cgi script. Basically, I set up a global variable --> $user. I want to use it in index1.html
- index1.html - simple html file
- apache.conf - I am not sure I did it correctly
Error
1. type http://localhost:81/index1.html in browser 2. error: Error during compilation of /var/www/test/sj3/public_html/index1.html: Global symbol "$user" requires explicit package name at /var/www/test/sj3/public_html/index1.html line 2. context: 1:2: 3:
4: code stack: /usr/share/perl5/HTML/Mason/Interp.pm:450 /usr/share/perl5/HTML/Mason/Request.pm:249 /usr/share/perl5/HTML/Mason/Request.pm:212 /usr/share/perl5/HTML/Mason/ApacheHandler.pm:94 /usr/share/perl5/Class/Container.pm:275 /usr/share/perl5/Class/Container.pm:353 /usr/share/perl5/HTML/Mason/Interp.pm:348 /usr/share/perl5/HTML/Mason/ApacheHandler.pm:874 /u开发者_开发问答sr/share/perl5/HTML/Mason/ApacheHandler.pm:828 (eval 34):8 -e:0
apache.conf
# Listen to other ports Listen 81 ServerAdmin webmaster@localhost DocumentRoot /var/www/test/sj3/public_html PerlSetVar MasonCompRoot /var/www/test/sj3/public_html/ PerlSetVar MasonDataDir /var/www/test/sj3/mason/ Action html-mason /cgi-bin/mason.speedy2 SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler
index1.html
print %user
mason.speedy2
#!/usr/bin/perl -w use strict; use HTML::Mason::CGIHandler; { our($user, %session); $user = "bla"; } my $h = HTML::Mason::CGIHandler->new ( data_dir => "$ENV{DOCUMENT_ROOT}/../mason", allow_globals => [qw(%session $user)], ); $h->handle_request;
If you must have site wide globals, you declare them with allow_globals in the apache handler, and initialize them in the root level autohandler.
print %user is a typo. It should be $user.
daotoad's suggestion is good, but it will take me a while to change the existing codes
The previous programmer has "/cgi-bin/mason.speedy2" containing all the global variables and package, then embedding /cgi-bin/mason.speedy2 in apache.conf (i.e. is similar to declare them in autohandler). 4. My question: Is /cgi-bin/mason.speedy2 running? If it is running, why the global variable $user is not defined?
精彩评论