kenpeter
kenpeter

Reputation: 8274

How to define a global variable with Mason

Intro

Local server

Files

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 /usr/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;

Upvotes: 1

Views: 3212

Answers (2)

kenpeter
kenpeter

Reputation: 8274

  1. print %user is a typo. It should be $user.

  2. daotoad's suggestion is good, but it will take me a while to change the existing codes

  3. 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?

Upvotes: 0

daotoad
daotoad

Reputation: 27183

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.

Upvotes: 2

Related Questions