http://qs1969.pair.com?node_id=842747

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

I have a cgi script that works well for one 'item' but now need to pass a number of items and process them one at a time.

I think the easiest way to do this would be to pass the list as a file to the cgi script. How do I pass a file to a cgi script please though the browser? I know to pass one 'item' its like

http://myurl/cgi-bin/upload.cgi?id=item1
Is it something similar for an entire file? And how might I go about accessing the file in a cgi script?

Thanks for your help!!

Replies are listed 'Best First'.
Re: passing a file to a cgi script
by Corion (Patriarch) on Jun 02, 2010 at 12:10 UTC

    See CGI about file uploads.

Re: passing a file to a cgi script
by marto (Cardinal) on Jun 02, 2010 at 12:14 UTC

    Do you mean file uploads (if so see Corions advice) or are you trying to automate a site using values stored within a text file?

      I'm trying to automate a site using values stored within a text file :)

        So you need to open the text file, read each set of values and pass them to your URL. Perhaps you want to use something like LWP or WWW::Mechanize (or similar if you need JavaScript support) to make your task easier.

Re: passing a file to a cgi script
by bradcathey (Prior) on Jun 02, 2010 at 13:25 UTC

    You say you want to pass a number of items to a script. There are several ways, depending on your point of origin. I get the sense that you want fluidity in your script with changing parameters. IMO, a file would be more cumbersome, where using the standard conventions of an HTTP post and CGI would offer greater ease and flexibility.

    Some options, in no order:

    1. If you are posting from an HTML form, this is already done for you in the guise of name=value pairs. You just have to use CGI to parse the string.
    2. You can provide the name=value pairs yourself (you started to show this). E.g., upload.cgi?name1=value1&name2=value2&name3=value3... There's no limit to the number of pairs.
    3. If you are supplying the name=value pairs via file, where does the file come from? Is it uploaded from your local computer via a form or FTP, or created on the server through an editor, e.g., Vim, or some admin HTML form
    4. Or could you use a configuration file and tap it with something like Config::Simple?
    5. Is it a text file or can the name=values be stored in a database?

    If you are calling the script from an HTML link or another Perl script, option #2 above would be the easiest. Messing with uploading and maintaining files seems kludgy.

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot