Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to convert an @ARGV to form based input?

by ig (Vicar)
on Aug 21, 2012 at 08:22 UTC ( [id://988638]=note: print w/replies, xml ) Need Help??


in reply to How to convert an @ARGV to form based input?

You have explained a bit about what you don't want but you haven't said much about what you do want, so I can only guess. Anyway, you seem to be having a problem accessing CGI parameters from your script. Here is one simple way to access a CGI parameter:

use strict; use warnings; use Net::DNS; use CGI; use CGI::Carp qw{fatalsToBrowser}; my $q = CGI->new(); my $dns = new Net::DNS::Resolver; if(my $domain = $q->param('domain')) { my $mx = $dns->query( $domain, 'MX' ); print "content-type:text/html; charset=utf-8\n\n"; print "$domain:\n\n"; foreach my $rr ($mx->answer) { print "<br />", $rr->exchange, ' [<b>', $rr->preference, "</b> +]\n"; } } else { die "missing required parameter: domain"; }

If you query this CGI script with a URL like:

http://mydomain.tld/dnsdig.cgi?domain=google.com

You should get something.

It is unusual, in my experience, to use unnamed parameters with HTTP requests. Whether using GET or POST, I always name my parameters. Note that this CGI script will work with both GET requests, with parameters in the URL or POST requests with parameters in the body, as commony submitted from HTML forms.

Hopefully, this will get you past your block, to see how query parameters can be accessed from within a CGI script.

Do read up on CGI and CGI::Carp

Replies are listed 'Best First'.
Re^2: How to convert an @ARGV to form based input?
by Anonymous Monk on Aug 21, 2012 at 08:36 UTC
    Or even
    use CGI; my $query = $ENV{QUERY_STRING} || $ENV{REDIRECT_QUERY_STRING} || '%68% +69.example.com'; my $domain = CGI->unescape( $query ); print CGI->header; print "$domain\n"; __END__ Content-Type: text/html hi.example.com
Re^2: How to convert an @ARGV to form based input?
by taint (Chaplain) on Aug 21, 2012 at 08:57 UTC
    Good advice @ig, and thank you very much for the reply.
    What I actually have, is a very large script that provides every option DNS queries permit. I built, and tested it as CLI (at the console/term). Now that it all works at the term, I want to incorporate a form field where the (@ARGV) is. Since the whole thing is so large, I simply posted one of the queries as an example. I have already finished all of the web based stuff, providing each query option as a
    <select> <option> </option> </select>

    Then providing additional options as checkboxes, or radio.
    so: the example I posted here appears as:
    <select> <option>MX</option> </select>

    the additional options are A, AAA, HINFO, IP6, TXT, etc...
    What I do want to do, is provide an empty form input field that allows the entry of a domain name to query those options for. meaning; replace the (@ARGV) I have now, with a form input field. As it sits now (without form fielsd(s), I can query the form via:
    http://mydomain.tld/dnsdig.cgi?somedomain.tld
    and it returns the MX records (using the example above with the (@ARGV). But I don't want the web clients location: to see, or be able to manipulate the query string -- hence the desire to put the form fields within the script itself, and my choice of using the POST method.
    I hope that helps clear things up a bit, and thanks again for your thoughtful reply.
    --Chris
    #!/usr/bin/perl -Tw
    use perl::always;
    my $perl_version = "5.12.4";
    print $perl_version;

      But I don't want the web clients location: to see, or be able to manipulate the query string -- hence the desire to put the form fields within the script itself, and my choice of using the POST method.

      Yeah, POST doesn't hide anything from the client

        No?
        I've got a whois form that is accessed by way of:
        http://whois.mydomain.tld/
        and no matter what you put into the location field of your browser after the /, you'll be returned with http://whois.mydomain.tld/ in your location: bar, and an empty form, waiting for you to place a Domain name, IP address, AS number, or RP into the field. The location bar never reveals the query, and can't be manipulated.
        So I'd have to respectfully disagree with your assertion.
        --Chris
        #!/usr/bin/perl -Tw
        use perl::always;
        my $perl_version = "5.12.4";
        print $perl_version;

      From CGI:

      It is possible for a script to receive CGI parameters in the URL as well as in the fill-out form by creating a form that POSTs to a URL containing a query string (a "?" mark followed by arguments). The param() method will always return the contents of the POSTed fill-out form, ignoring the URL's query string. To retrieve URL parameters, call the url_param() method. Use it in the same way as param(). The main difference is that it allows you to read the parameters, but not set them.

      So, you can easily ignore parameters from the URL when using CGI. If you are parsing the CGI inputs otherwise, you will have to look to the tools you are using to find out what your options are. When you are delivering the blank form, you can ignore parameters entirely, if that suits you.

      For an empty form input field, I would use something like:

      <form method="POST" action="http://mydomain.tld/dnsdig.cgi"> <input type="text" name="domain"> <input type="submit" value="Submit"> </form>

      If the user enters a URL that refers to your CGI script and includes a query string in the URL, you could detect this and issue a redirect to a URL without a query string.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found