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

Hello, I'm trying to figure out how to make adjustments to the following code to run the requests through the command line instead of using a form(ie. mail.cgi?makemail, or mail.cgi?what=makemail)...any help would be greatly appreciated! Thanks! ~Lis
#!/usr/bin/perl require 'r-mail.cfg'; require 'r-mail.common.cgi'; require 'r-mail.sub.cgi'; $use_cgi = 1; &readform; &confirm if ($action eq 'confirm'); &actually_send_mail if ($action eq 'moremail'); &set_mail if ($what eq 'setmail'); &set_sig if ($what eq 'setsig'); &set_add_list if ($what eq 'setaddlist'); &set_del_list if ($what eq 'setdellist'); &set_list if ($what eq 'setlist'); &set_pass if ($what eq 'setpass'); &make_mail if ($what eq 'makemail'); &change_sig if ($what eq 'changesig'); &add_list if ($what eq 'addlist'); &set_del_list2 if ($what eq 'setdellist2'); &del_list if ($what eq 'dellist'); &set_list2 if ($what eq 'setlist2'); &change_list if ($what eq 'changelist'); &change_pass if ($what eq 'changepass'); #&check_pw; &render_admin; exit; sub render_admin { print <<"END"; <html> <head> <title>Notable Web CBL: Multi-Mailer</title> </head> $head $pageadmin $mid <center><table BORDER=0 CELLSPACING=0 CELLPADDING=10 BGCOLOR='#663366' + > <tr><td> <center> <table BORDER=0 CELLSPACING=3 CELLPADDING=3 WIDTH='100%' BGCOLOR='#FFF +FFF' > <tr><td> <center> <h1>R-Mail</h1> </center> </td></tr> <tr><td> <center><form action = '$cgiurl/r-mail.cgi' method='post'><input type= +'hidden' name='what' value='setmail'><input type='submit' value='Comp +ose a Message'></form></center> </td></tr> <tr><td> <center><form action = '$cgiurl/r-mail.cgi' method='post'><input type= +'hidden' name='what' value='setsig'><input type='submit' value='Chang +e Signature File'></form></center> </td></tr> <tr><td> <center><form action = '$cgiurl/r-mail.cgi' method='post'><input type= +'hidden' name='what' value='setaddlist'><input type='submit' value='A +dd New Mailing List'></form></center> </td></tr> <tr><td> <center><form action = '$cgiurl/r-mail.cgi' method='post'><input type= +'hidden' name='what' value='setdellist'><input type='submit' value='D +elete a Mailing List'></form></center> </td></tr> <tr><td> <center><form action = '$cgiurl/r-mail.cgi' method='post'><input type= +'hidden' name='what' value='setlist'><input type='submit' value='Edit + Existing Mailing List'></form></center> </td></tr> </table></center> </td></tr> </table></center> <br> $footer </body> </html> END exit; }

Replies are listed 'Best First'.
Re: Command Line Question
by vladb (Vicar) on May 08, 2002 at 18:58 UTC
    It should be possible for you to set a few environment variables to simulate the environment that your CGI script is executed in by the server responding to client request (via browser). What I normally do is set these variables:

    • QUERY_STRING (set this to standard URL query string -- whatever comes after the ? character)
    • HTTP_COOKIE (if you deal with cookies)
    • SERVER_NAME (optional)

    After that simply run your script from command line as you'd normally do. Normally any methods you use in your script to process CGI data (parameters etc) should work fine.

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
Re: Command Line Question
by xtype (Deacon) on May 09, 2002 at 02:05 UTC
    While some people might say "use CGI.pm or die"...
    I will just say
    export REQUEST_METHOD "POST" or setenv or set REQUEST_METHOD = "POST" and then ~> perl -wT mail.cgi what=makemail otherparam=this
    Also, for debugging, You can use -c to parse the script (without running it) to check for errors.
    ~> perl -wT -c mail.cgi what=makemail otherparam=this