views:

788

answers:

6

I want to maintain state in my Perl web app. How can I do this effectively? I looked at CGI::Session but it says that it doesn't work well with UTF-8 pages, which is a requirement. I'd also like to be able to pass some basic information to another Java application running on the Glassfish app server, so people aren't forced to login to both apps, for example. How can all this be managed?

+5  A: 

The basics of sessions are that you need a place to hold the session data (the store) and a way to store and retrieve the session data. While some frameworks call that The State, it really boils down to having a session key (or session ID) and passing it back to your app via either a cookie or a URL parameter.

Your store can be anything that can hold the data for you. Some examples are: a flat file, a dbm file, a DBMS,or an in-memory cache of some type.

The most common implementation, which is used by CGI::session and Apache::Session is to have three fields in each record inside the store: session_id,session_data, expires_time.

The session modules on the CPAN take care of loading your session at the beginning of the request, and storing it back at the end.

Len Jaffe
+2  A: 

it says that it doesn't work well with UTF-8 pages

Can you explain what this means? Where does "it" "say" that?

AmbroseChapel
It's in the docs: http://search.cpan.org/~markstos/CGI-Session-4.38/lib/CGI/Session.pm#A_Warning_about_UTF8
Gaurav
+2  A: 

The bug referenced in the documentation for CGI::Session is marked as "resolved". That one was about utf-8 in the database.

It seems that you still should not use 'use encoding utf-8'. But why would you do that in the first place? "perldoc encoding" says "allows you to write your script in non-ascii or non-utf8". WTF?

Having your resulting HTML utf8-encoded does not seem to be an issue when using CGI::Session.

innaM
A: 

Find a Perl web framework that handles sessions for you.

For example Catalyst. It will completely abstract out user sessions so that you don't really even have to think about it. See a tutorial on how to use sessions with catalyst.

If you're programming a website in Perl, you should really be using this framework anyways.

superjoe30
A: 

Hm, if it is not too late. CGI::Session works just fine with any data. It depends on the serialization format that you use for your session and how you store them in your database. I would suggest also a combination with CGI::Simple for which I added UTF-8 form-data handling.

Is your connection to the database UTF-8 enabled?

for MySQL you can use: SET NAMES UTF8; as the very first statement after connecting;

Do you store store your session in a text field with some different encoding?

Just take a little care with such things and you will be fine.

You can look at some examples of usage here.

Berov
A: 

Your best bet would be to generate some sort of session key and store it in a cookie on the browser. Then, use that key to store information in either a database or memcached that both can access.

R. Bemrose