Raymond,

I think you're on the right track by considering global variables but
you have to be careful.  See comments below.

Raymond Wan wrote:
>
> What is a global variable in Mason?  Are global variables accessible by 
> all components in a request, all requests from a single user (this one 
> doesn't make sense), or global for all users?
>   
A global variable is one that is available from all components and it's
value may persist across all requests served by the same perl
interpreter/web server process.

> Even worse, if User 1 sets the value to "100" upon reading index.html 
> and user 2 sets it to "200", if user 1 goes to next.html, is it foolish 
> to rely on the fact that x must be "100"?
>   
In most cases yes it is foolish to assume that the value will persist
from request to request or user to user.  The foolishness of this
depends on your configuration.  If you're running an Apache web server
with a normal configuration there are actually a number of apache
processes running that serve requests.  Each one of these processes
contains it's own perl runtime with it's own memory.  When you store
data in a global variable it is only stored in the memory of the process
that served the request.  This value will persist for every other
request that this process serves.

In my experience Apache usually serves consecutive requests from the
same IP address with the same process.  Thus if my first request hit
process #3456 then my second request will most likely (no guarantees)
hit process #3456.  Any requests from your IP address may also go to
that process or they may go to process #3460 which has a completely
different memory allocation.

If your apache configuration only spawns one process to handle requests
then the memory will persist for all requests and all users and relying
on this will not necessarily be foolish.  But this type of configuration
can be perhaps a different kind of foolishness in a high traffic
configuration.
> In the Mason Book, they seemed to have used user names and DB access 
> ($User and $Schema) as a global, but they "limit their lifetime via the 
> use of local()".  In the context of Mason, what does local have the 
> effect of doing?
>   
Declaring a variable using local creates a blank version of a variable.
This guarantees that code following the 'local' declaration will have a
clean slate.  This is a fairly standard way to use global variables.  In
my experience there is a top level autohandler or system handler which
clears out every global variable that exists.

With all this in mind you can use a combination of cookies and global
variables to store your id.  Storing the id in a cookie ensures that the
browser will send the value with each request.  Your code can store the
value of the cookie in a (cleared out) global variable which will be
accessible by any component for the duration of the request.

If you have the time I would recommend looking into the Apache::Session
module.  This allows you to store various information about a user
session in a location that will persist for the same user across
requests.  The storage location can be in a relational database or other
server side persistent container (e.g.: file)

Good Luck,

-- BenRifkah


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to