Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Passing Data between Scripts

by filmo (Scribe)
on Jan 05, 2004 at 03:08 UTC ( [id://318751]=perlquestion: print w/replies, xml ) Need Help??

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

I have an originating page called "original.html". On it is a form field for file upload, etc. It calls a script called upload.pl

When upload.pl is done doing it's thing, I want it to rediret to a different page, final.pl and pass along some structured data.

final.pl will read that data and display the results on the users browser.

I've been playing with WDDX and can successfully create the packet of info. However, when I send to to the 'final.pl' page via the 'Location: final.pl?wddx=$wddx_packet' It fails for a couple of reasons. (1) WDDX packet is not URI encoded. (I've taken care of that problem with the URI module). (2) Seems like only the first 250 characters or so are passed via the URL.

Question: How do I have it so the user starts on 'original.html', the processing is done by upload.pl, but the results and the rest of the html code are shown on final.pl?

I'd prefer not to have to build some sort of SOAP solution if possible and I'm sure this must have been done before, so before I reinvent the wheel...


--
Filmo the Klown

Replies are listed 'Best First'.
Re: Passing Data between Scripts
by davido (Cardinal) on Jan 05, 2004 at 03:19 UTC
    You can send data between scripts via the URL (your WDDX packets, for example), but as you've found, you eventually may run out of room (around 250 characters).

    When you start needing to hold onto more info than that, you might want to store the info in temporary files, and just pass an encrypted session key in the URL.

    The documentation for CGI.pm has some examples of maintaining state. Also the Mouse book (CGI Programming with Perl, 2nd Edition from O'Reilly) has a lot of great discussion on maintaining state.

    There are as many strategies for maintaining state with CGI as there are "Ways to Do It(tm)" in Perl.


    Dave

Re: Passing Data between Scripts
by cLive ;-) (Prior) on Jan 05, 2004 at 05:54 UTC
    The GET method (query string) is indeed limited. I think the length is browser dependent and is usually anything from 255 chars to 1Kb.

    If you have access to a DB, store the data there, then just forward the key as a hidden field or cookie, and use that to retrieve the data when needed.

    If you don't, why not just create a temporary file with the data in and just pass an identifier for the file to the next script?

    Or, a nice messy way would be to eval final.pl from within upload.pl :) - or just combine them into one script.

    .02

    cLive ;-)

      Although query string length limits provide one reason to use POST instead of GET, these two methods also have different meanings. Use GET when the request doesn't change anything (such as a search engine query) and POST when the request has an effect (such as updating a database, sending email). See Methods GET and POST in HTML forms or section 9 of RFC 2616.

      If you can't combine your code into one script, you should investigate some kind of session mechanism.

        Interesting. Times like this I feel "Internet Old".

        "Eee lad, back int our day, we didn't have no fancy POST method. We lived and breathed GET. We 'ad it tough. Never mind your fancy mp3 uploads. We couldn't even upload JPEG or GIF files. Well, we wouldn't have uploaded GIF files anyway cause of fear of being sued down't drain by Unisys. But we were 'appy. Happy and poor, because we were poor. Lad.

        cLive ;-)

        --

Re: Passing Data between Scripts
by iburrell (Chaplain) on Jan 05, 2004 at 21:39 UTC
    There is a limit to the size of the data that can be passed through GET query string. Redirecting to a POST does not work.

    One solution that people didn't mention is running the final.pl script from the upload.pl script. If query.pl is a CGI script, then you can pass the parameters on the command line. Faking a POST is harder because it requires writing to the child process.

    As long as the upload.pl does not write anything to the client, the final.pl script can write the entire response.

    my $query = 'wddx=' . $packet; exec('final.pl', $query);
Re: Passing Data between Scripts
by Joost (Canon) on Jan 05, 2004 at 23:56 UTC
    IF both scripts are on the same server, you can do something like:
    # upload.pl use vars qw($data); $data = "data to be shared"; require "/path/to/final.pl";
    And then..
    # final.pl use vars qw($data); print $data; # or do something more interesting.
    The require statement will just load the "final.pl" script and run it. The $data variable will be shared between the files because it's a global variable.

    This will NOT redirect your browser, so it might cause some problems, but it is much easier to share data this way.

    Also note this assumes the $data variables are in the same package.

    HTH,
    Joost.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found