passing a reference to a hash to another page

C

ccc31807

This is a web front end to a database, served via Apache using SQLite
using CGI.

The key requirement is that the app displays the data to the user and
prints it at the user's request, which happens infrequently.

I can print a document by:
- running a query to get a hash ref
- processing the hashref to get an appropriate data structure
- opening a file
- printing the data structure to a file
- closing the file, and
- returning a link to the file to the user

I can also display the data by:
- running a query to get a hash ref
- processing the hashref to get an appropriate data structure, and
- printing the data structure to the screen

What I need is a button on the display that will allow the user to
print a document using the same hashref. I don't want to run the query
again, and don't particularly want to store the data (using Storable,
perhaps), but to pass the hashref to another page via an HTTP request,
and have the second page open a file, print to the file, close the
file, and return a link to the user.

Unfortunately, the hashref doesn't persist between invocations of the
script and new HTTP requests. For example, I can do this:
<a href="mydata?what=printcsv&data=HASH(0xDEADBEEF)"
target="_blank">Print a file</a>

But all I get is the value 'HASH(0xDEADBEEF)". I can't dereference it
to get at the data.

Seems like I should be able to tell the program to hold onto the
hashref between invocations of the script, but I can't seen to figure
out how. Ideas?

Thanks, CC.
 
R

Rainer Weikusat

[...]

What I need is a button on the display that will allow the user to
print a document using the same hashref. I don't want to run the query
again, and don't particularly want to store the data (using Storable,
perhaps), but to pass the hashref to another page via an HTTP request,
and have the second page open a file, print to the file, close the
file, and return a link to the user.

Unfortunately, the hashref doesn't persist between invocations of the
script and new HTTP requests. For example, I can do this:
<a href="mydata?what=printcsv&data=HASH(0xDEADBEEF)"
target="_blank">Print a file</a>

But all I get is the value 'HASH(0xDEADBEEF)". I can't dereference it
to get at the data.

Seems like I should be able to tell the program to hold onto the
hashref between invocations of the script,

This is impossible except if you're using a persistent perl
interpreter (eg mod_perl) and the next request of the user happens to
be processed by the same process and the hashref hasn't been
deallocated in the meantime, say, because an entry in a 'global' hash
table points to it. You'll either have to store it locally, possibly,
using some module which provides session support or send it to the
user and have him send it back, for instance, putting a textual
representation of the hashref in a hidden form field.

Possibly helpful:

http://search.cpan.org/~sherzodr/CGI-Session-3.95/Session/Tutorial.pm

NB: I haven't used the module myself.
 
C

ccc31807

This is impossible except if you're using a persistent perl
interpreter (eg mod_perl) and the next request of the user happens to
be processed by the same process and the hashref hasn't been
deallocated in the meantime, say, because an entry in a 'global' hash
table points to it. You'll either have to store it locally, possibly,
using some module which provides session support or send it to the
user and have him send it back, for instance, putting a textual
representation of the hashref in a hidden form field.

Thanks, Ranier and Ben.

I've decided to hit the database every time a user requests data,
regardless of the purpose.

I'm sending the search parameters via a form, and I have modularized
both the presentation of the form and the response to the form.
Instead of printing the output back to Apache, I'll just open a file
and send it to the file.

Lest you think that I was confused, let me assure you that I was
indeed confused. The idea that I had was that, since the data resided
somewhere in memory, and since I had the address of that memory,
instead of losing the reference to the hash I would reuse it. I do
this routinely with scripts that I have written for the purpose of
printing reports, i.e., printing them directly without displaying them
to the user, and thought I could both print the data to HTML output
and print the data to a file afterward with an intervening HTTP
request, but just couldn't think it through.

Thanks for your time and help -- I appreciate it.

CC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top