passing a reference to a hash to another page

Discussion in 'Perl Misc' started by ccc31807, May 4, 2012.

  1. ccc31807

    ccc31807 Guest

    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.
    ccc31807, May 4, 2012
    #1
    1. Advertising

  2. ccc31807 <> writes:

    [...]


    > 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.
    Rainer Weikusat, May 4, 2012
    #2
    1. Advertising

  3. ccc31807

    ccc31807 Guest

    On May 4, 6:20 pm, Rainer Weikusat <> wrote:
    > 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.
    ccc31807, May 7, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Cohen
    Replies:
    3
    Views:
    270
    Andrew Durstewitz
    Jul 15, 2003
  2. rp
    Replies:
    1
    Views:
    520
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    617
    David A. Black
    Jul 2, 2008
  4. Davide Bruzzone
    Replies:
    2
    Views:
    240
    Davide Bruzzone
    Aug 4, 2003
  5. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    118
    Steve
    Sep 28, 2003
Loading...

Share This Page