http://qs321.pair.com?node_id=273022

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

Follow Monks:
I am tranforming some ASP code to cgi using perl. The ASP code stores values in session variables. I would like to know if there is a similar way to do this with perl.
ASP code if session(strname)="" then session(strname) = 0 end if

desired perl code: to be able to use a variable throughout an entire session

-thanks

Replies are listed 'Best First'.
Re: session variables in cgi
by MrCromeDome (Deacon) on Jul 10, 2003 at 15:52 UTC
    You may wish to look at either CGI::Session or (if you're going to be using mod_perl) Apache::Session. They are two of the best ways I know of for working with sessions.

    You didn't ask about this, but you may wish to avoid "transforming" your ASP code to Perl. Writing an identical looking site with similar features is one thing, but when you try line-by-line conversions (which is what I gathered from "transform") from one language to another (especially with Perl) you'll often times create a lot of headaches for yourself. Learn to use Perl's features instead of looking for ways to recreate ASPs features. You'll find yourself extremely impressed by the power and simplicity Perl will provide you :)

    Cheers!
    MrCromeDome

      Apache::Session doesn't require mod_perl. In fact, it doesn't even require Apache :)

Re: session variables in cgi
by cees (Curate) on Jul 10, 2003 at 16:30 UTC

    If you are familiar with the structure and features of ASP, then I would recommend looking at Apache::ASP. This is a framework that has borrowed the good things from ASP and added a lot more as well. A quote from the Apache::ASP website:

    Apache::ASP provides an Active Server Pages port to the Apache Web Server with Perl scripting only, and enables developing of dynamic web applications with session management and embedded Perl code. There are also many powerful extensions, including XML taglibs, XSLT rendering, and new events not originally part of the ASP API!

    It won't be possible to copy your ASP code exactly, but it should be easy enough to translate it.

    - Cees

      Hear! Hear! Apache::ASP is great! Your session management is simple, just as with windows ASP.
Re: session variables in cgi
by Excalibor (Pilgrim) on Jul 10, 2003 at 16:32 UTC

    Hi there!

    OK, of course, what you are asking for is:

    # a hash (most important data type!) # ASP code # if session(strname)="" then # session(strname) = 0 # end if my %session = give_me_my_session( $this_is_who_i_am ); if ( not exists $session{$strname} or $session{$strname} eq '' ) { $sesssion{$strname} = 0; }

    Of course, the trick is those give_me_...() and $this_is_... A nice trick if you are using Apache with mod_perl is using Apache::ASP. It gives you a complete ASP environment to program in Perl. It's almost a one-by-one map of ASP objects, thus translating an ASP to a Perl script could almost be done automatically (add sigils where they correspond, change . for -> in ASP objects method calls, and the VB or JS constructs to Perl ones.

    By the way, if you can start from scratch, your script will be much cleaner and idiomatic (if you're good at Perl), and you can actually leave the whole ASP mess out and use CGI::Session as suggested by another monk.

    If you rather not start all over, Apache::ASP can come very handy (get it from CPAN, of course).

    Good luck,

    --
    our $Perl6 is Fantastic;