in reply to Re: Clean URLs and CGI.pm
in thread Clean URLs and CGI.pm

But it really isn't the result of an ISINDEX. (I think). Here's an example:

Let's say I'm writing a search script against some database. You can search, you can follow links from the results page to give you more results, you can go to an "advanced search" page and set all sorts of options. So normally, this will be a standard GET or POST setup with multiple values.

However, because I'm a difficult person, I want it to also accept one parameter without a key-value pair (It will know which field this value belongs to). For example, most search engines have many options, but will work just fine with /search.pl?q=search+terms. I don't want the "q=" or whatever though. I want users who are in the know (and HTML writers who want VERY simple links) to have: "search.pl?term"

So I can't just use param(), because I don't know which manner the script is being called by. The code I posted works just fine, I just wanted to make sure I wasn't making some grevious error or missing something obvious.

Did I understand your reply correctly?

Replies are listed 'Best First'.
Re: Re: Re: Clean URLs and CGI.pm
by Trimbach (Curate) on Jun 11, 2001 at 21:40 UTC
    If you want your CGI called with "search.pl?term" that's fine... all you have to do is $q->param(keywords) in your CGI to access whatever follows the "?" in your URL. If someone calls your script like this "search.pl?field1=term" then the "term" value will appear in $q->param(field1). Just set some if/then's to decide which is which, maybe like this:
    if ($q->param(keywords)) { $field = $q->param(keywords); } else { $field = $q->param(field1); }
    Or maybe I don't understand what you want. The 3 main ways of getting data into CGI.pm are via ISINDEX, POST, and GET. Since POST and GET both boil down to key/value pairs they are (to CGI.pm at least) interchangeable. ISINDEX is just a value without a key and is handled in param(keywords). Now why can't you use param again?

    Gary Blackburn
    Trained Killer

      Just set some if/then's to decide which is which,

      Which would be the code I posted originally. Please look at it, I _AM_ using param. I was just saying I can't assume I'm in only one mode.

      I was really asking if anyone else had done this (I'm not so original as to be the first to do this), and if I was leaving myself open to any security holes and the like, or missing any features of CGI or common idioms to do this.

        Trimbach is politely trying to point out that you are missing both a common idiom and a feature of CGI.pm, namely that a query string without ampersands is treated as an ISINDEX search and the values can be retrieved with the keywords() method, though from my reading it's a list you'll need to join.

        A small change to your code would make it work. I expect something more elegant could be done with a [0], but not by me :(

        $page->param('field',join('',$page->keywords)) unless $page->param('field');