Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send an array to another CGI script? 4

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I have a CGI script written in Perl that prints a bunch of lines of data to the users web browser. Each line is stored in an array called @stuff. I want to input a form where the user can narrow down the results further. This would require that I send the @stuff array to the next CGI script. Is it possible to send an entire array to another CGI script?

I could save that array to a file, and then open that file in the next CGI script, but I was hoping that there was a better way than that.

Thanks,
Chris
 
As far as I know by default variables are destroyed when a script finishes

I think there's a module called Storable which might suit.

Your best bet is to store the array in a database indexed by the session id if you have one

HTH
--Paul
 
Yea, you can use Storable to ouput the variable to a file on disk, track the file name or ID with a cookie and have the next script open it up.

Or you can pass them as GET arguments.

var=1&var=2&var=3

CGI.pm will parse var into an array nicely.

But i'd go with a session, that GET string is 'quick hack get it done' type stuff.
 
How is using the Storable module different from writing the output to a text file using the open function? I'm very new to Perl.

Thanks,
Chris
 
Storable actually will output a complex variable into a scalar, store it, retrieve it and parse it back into a hash for you.

Sure, you can open a file and output each variable to it but once you get to hashes or muli-level arrays your up the creek without a paddle.

Storable is along the idea of 'why rebuild the wheel if you do not have to.'

 
It sounds like you need some general session management. Look into CGI::Session. At its simplist, it works roughly like that, storing session info in a file on the server. Check CGI::Session::Tutorial on cpan for a very good intro reference.

________________________________________
Andrew - Perl Monkey
 
Yea, icrf is pointing to your real issue, session management.
 
Thanks guys. I'm using the Storable module right now and it works great. The only problem is that it writes out the variables to the same file everytime. I would like to have it write the variables to a file named after the session ID (like mentioned above). I want to use something like this...

$sessionID = ?????
$file = $sessionID.txt
store \@stuff, $file;

I'll read up on the CGI::Session:Tutorial and see if that explains it.

Thanks,
Chris
 
Depending on the size of the array, you could possibly write it to a cookie too.

But what I generally do is output the second form via the first CGI script, thereby allowing me to write my array, etc., to JavaScript variables or hidden form fields so that I can play with the data easily client-side. Then there's no need to write files to your server in order to maintain state, which saves on a lot of I/O. Let the client save the state for you.

The only caveat is that you'll have to sanitize all prior input which is passed along again at each step just in case some malicious types play where they shouldn't.

Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Parsing this info from hidden fields and cookies actually doesn't buy you anything I/O wise since you are passing redundant info on each connection.

I've done it both ways over the years and found server side sessions to be more flexibile, less error prone, far more secure and generally more reliable then client side session maintance.

A great example is shopping carts, if you use client side data to manage your shopping cart you have to work around all sorts of lameness like duplicate purchases (page reloading), users messing with the data (you must double check purchases against the catalog at every step to validate all submitted data) etc etc etc.

Your Mileage May Vary, I left client side sessions behind in 1999 and never looked back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top