Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

mason request scope variable

by jeteve (Pilgrim)
on Jan 16, 2007 at 11:43 UTC ( [id://594893]=perlquestion: print w/replies, xml ) Need Help??

jeteve has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks

I plan to use HTML::Mason to develop a website, and I'm a bit struggling with session handling

What I'd like to do is to retrieve the session in the init block of the autohandler, save it in the cleanup block of this same component and the most important, make it available under the name of $session in every component used.

This $session variable should be global on a request scope.

Do you know how to set that up ?

Thx !!

-- Nice photos of naked perl sources here !

Replies are listed 'Best First'.
Re: mason request scope variable
by themage (Friar) on Jan 16, 2007 at 13:02 UTC
    Hi jeteve,

    I use Mason for almost everything I write to web. The way I usually do this is by passing the session variable as parameter to the $m->call_next(session=>$session);

    However, I don't use <%init>, <%once>, <%cleanup> or <%filter> (I used <%filter> once). The main reason I don't use them is that the code turn into a less predictable code.

    For instance, in this code:
    <%cleanup> print "Cleanup"; </%cleanup> <%init> print "Init"; #This is an alias to $m->out(); </%init> <%filter> print "Filter"; </%filter> <%perl> print "Perl"; </%perl> <%once> print "Once"; </%once>
    Can anyone say which of the blocks will be called first? I don't, and don't want to be always looking for the next (or previous) code block, so I just use (most of the time) the <%perl> and <%args>. You can do everything you need using just this too, and your code is a lot more linear.

    For situations identical to yours, my code usually looks like (in the autohandler):
    <%perl> my $session=$m->comp('/mason/session/session.mas'); $m->call_next(session=>$session); $m->comp('/mason/session/session.mas', action=>'save', session=>$sessi +on); </%perl>
    Hope this helps you.

Re: mason request scope variable
by chakram88 (Pilgrim) on Jan 16, 2007 at 14:54 UTC
    Yes, Chap 11 of the Mason Book is a good reference for a Session Handling recipe.

    We deal with sessions in a slightly different manor (using a DB to store the session data) --- but to answer

    This $session variable should be global on a request scope.
    Do you know how to set that up ?

    We declare it in package HTML::Mason::Commands with use vars $session

Re: mason request scope variable
by Anonymous Monk on Jan 16, 2007 at 12:54 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://594893]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found