Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

CGI::Session - trouble passing variables

by Lori713 (Pilgrim)
on Mar 01, 2004 at 20:05 UTC ( [id://333033]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to learn how to use CGI::Session. After extensive reading all day, googling, POD, etc., I'm at a loss as to why the following won't pass a variable to the next .pl that is called. (I want to use CGI::Session using a hidden field instead of a cookie). The first script runs fine, using HTML::Template, and prints the following at the bottom:

     my sid is: 004493c708b377edb6b73dbc5f6b9e61
     my f_name is: CGI::Session::File=HASH(0x1b303c)

The second script opens up Excel nicely but gives me an error message. NOTE: $sid is passed via a hidden field in a form, so I'm expecting it to move to the next script via &call_vars which moves all my other variables. Another note: if $sid didn't exist, use strict would complain.

However, it shows my $sid as being "1" and not the big long alphanumeric string I would have expected. So, since it doesn't appear that the $sid is being passed, I'm not really expecting f_name to show up... but I also wasn't expecting a param error either...

     my sid is: 1
     Software error: Can't call method "param" without a package
     or object reference at /local/.../SCRIPT2.pl line 12.

SCRIPT1 (which contains a button that calls SCRIPT2)

#!/usr/local/bin/perl5_8 use strict; use HTML::Template; use CGI::Session; use CGI ':standard'; my $CGI = CGI->new; &call_vars; #this calls via EXPORT all my variables like $sid $|=1; print $CGI->header; my $session = new CGI::Session("driver:File", undef, {Directory=>'/tmp +'}); $sid = $session->id(); $session->param('f_name', 'Sherzod'); #test variable #stuff that sets up template, generates @loop_data for template, etc. +snipped out $template->param(passdata => \@loop_data,); print $template->output(); print "<br>my sid is: $sid<br>"; print "<br>my f_name is: $session<br>";

SCRIPT2

#!/usr/local/bin/perl5_8 use strict; use HTML::Template; use CGI::Session; use CGI ':standard'; my $CGI = CGI->new; $|=1; print $CGI->header(-type => 'application/vnd.ms-excel'); print "<br>my sid is: $sid<br>"; my $f_name = $session->param('f_name'); #<=== Line 12 print "<br>my f_name is: $f_name<br>"; my $template = HTML::Template->new(filename => "SCRIPT2.tmp +l", associate => $CGI, loop_context_vars => 1, global_vars => 1, associate => $session );
Any clues for the clueless? TIA...

Lori

Replies are listed 'Best First'.
Re: CGI::Session - trouble passing variables
by valdez (Monsignor) on Mar 01, 2004 at 22:13 UTC

    Sorry, your code doesn't compile:

    > perl -c SCRIPT2.PL Global symbol "$sid" requires explicit package name at SCRIPT2.PL line + 10. Global symbol "$session" requires explicit package name at SCRIPT2.PL +line 12. Global symbol "$session" requires explicit package name at SCRIPT2.PL +line 20.
    I use CGI::Session every day and it works real well for me.

    Ciao, Valerio

      As I mentioned in the initial post, both $sid and $session are carried through and re-called by the function &call_vars (and my use strict would have complained loudly about undeclared variables). It works very well for me on every other variable I move from one page to another. However, maybe I shouldn't try to carry $sid and $session like the other variables even though they're scalar variables and the hidden field method I was using works well.

      I was considering CGI::Session because I needed to pass along a referenced array used in my HTML::Template. Hidden fields would not work for a referenced array, so I was looking for something to maintain persistent state. Thanks for taking a look at it.

      Lori

        You're not trying to pass $session through the form right? You're supposed to retrieve the session using $session = CGI::Session->new('driver:File',$sid,{Directory=>'/blah'};. Only the session id should be passed to the page.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: CGI::Session - trouble passing variables
by castaway (Parson) on Mar 01, 2004 at 21:46 UTC
    A small clue, last I attempted to use CGI::Session like this, I failed completely, and declared it to be broken. No idea where or how exactly, but upon asking others in the CB, it appears I'm not the only one.

    So my advice would be 'use something else'.

    C.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found