CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
"use CGI" and @ARGV; how to pass...

 



S_Shrum
User

Oct 21, 2001, 12:10 AM

Post #1 of 13 (4542 views)
"use CGI" and @ARGV; how to pass... Can't Post

I typically write my scripts so that they may be used both directly and in EXEC CMD calls. However, when the script is called via the latter, I am finding that the $query list is empty.

I have looked over the PerlDoc on CGI but see no reference to using this module to get the contents of the @ARGV.

I assume that it should be possible to pass @ARGV to it, I just don't know how.

TIA

Sean Shrum
sean@shrum.net
www.shrum.net


mhx
Enthusiast / Moderator

Oct 21, 2001, 2:17 AM

Post #2 of 13 (4540 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

You don't need to get the contents of @ARGV because CGI.pm already does this for you. Have a look at the section on Debugging of the CGI manpage, for example. As I mentioned in another thread, don't try to parse all kinds of input to the script unless you have to.

Just ask if you run into specific problems.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Oct 21, 2001, 2:25 PM

Post #3 of 13 (4532 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

I looked over the debugging section in the manpage but @ARGVs are not working. The CGI mod is working beautifully for "URL query strings"; meaning, things that are passed with the URL.

The problem, more definitively, is that I am attempting to call the script via a SSI (in which case the URL query string in blank). As a result the $query object has no keys when you attempt to print them in a foreach loop.

I read the section about passing a file that contained params to the $query (http://www.perldoc.com/perl5.6.1/lib/CGI.html#CREATING-A-NEW-QUERY-OBJECT-FROM-AN-INPUT-FILE) and thought "What the heck, let's try that.", so I attempted to pass the ARGV array. This seemed to sort of work. When I did this, I got only the first value of the @ARGV but no more (which I sort of expected to happen). CGI, when evoked in this method, looks for newline to seperate each pair. I was thinking of just constructing a string of the ARGVs like:

$ARGV[0]\n$ARGV[1]\n$ARGV[n]\n...

manually adding in the newline, but thought that I was dwelling too much into the issue and that CGI should handle this without me having to hack it to work. Unfortunately, this is not what I am seeing.

The EXEC CMD in the SHTML doc looks like this (each pair seperated with a <SPACE> and each key/value seperated with a "="):


Code
<!--#exec cmd="/a/home/shrum/cgi-bin/cgi_test.pl sort=Date max=10 slice=1"-->

By using:


Code
$query = new CGI;

and calling the script from within a SSI page, the $query is blank.

(One note of interest: If I call the SSI page and supply a URL query string with it like: http://someserver/cgi_test.shtml?test=1, the $query will then list those URL query string parameters: TEST -> 1.)

By using:


Code
$query = new CGI(@ARGV);

and calling the script from within a SSI page, the $query returned:


Code
sort -> Date

so you can see where I was going with this.

The script can be viewed here: http://www.shrum.net/cgi_test.pl.txt

Am I using/calling the CGI correctly or is this something that CGI doesn't deal with directly (but should)?

TIA

Sean Shrum
sean@shrum.net
www.shrum.net


Kanji
User

Oct 22, 2001, 1:37 AM

Post #4 of 13 (4523 views)
Re: &quot;use CGI&quot; and @ARGV; how to pass... [In reply to] Can't Post

CGI.pm only goes into debugging mode when a key enviromental variables are missing, which I'm guessing are being inherited from the SSI page instead, effectively bypassing debugging mode.

You can probably fix that be unsetting %ENV before you execute your CGI which a cmd string like ...


Code
  env - cgi-test.pl sort=date max=10 slice=1

... or explicitly have CGI.pm read its input from @ARGV.

You were really close to getting the latter working, but CGI.pm expects it's input to be a single URLish string rather than an array (which is why only the first param was showing up).

In your case, you could've seperated your command line arguments with ampersands instead of spaces, or joined them in the script itself ...


Code
  $query = new CGI ( join "&" => @ARGV );





S_Shrum
User

Oct 22, 2001, 2:02 AM

Post #5 of 13 (4520 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

You'd think that CGI would deal with ARGV directly but it's good to know. I spent way too long trying to get the ARGV in without hacking it to work.

MHX is looking into this aspect of it.

Thanks for the snippet...it worked perfectly.

Sean Shrum
sean@shrum.net
www.shrum.net


mhx
Enthusiast / Moderator

Oct 22, 2001, 10:46 AM

Post #6 of 13 (4516 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

The following works absolutely great for me. There's no need to care about @ARGV.

Here goes the SHTML:

Code
1  <html> 
2 <head><title>SSI Test</title></head>
3 <body>
4 <!--#include virtual="cgi-bin/ssi.cgi?test=foo&xyz=bar"-->
5 </body>
6 </html>

And this is the ssi.cgi, without any SSI-specific specials:

Code
#!/bin/perl -w 
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;

my $q = new CGI;
print $q->header, $q->Dump;

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Oct 22, 2001, 11:52 AM

Post #7 of 13 (4514 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

A thought that just came to mind...

In your scenerio above, what is displayed (or placed into $q) if you append a URL query string on to your shtml call???

Do you get both sets of params or just the params in the script call???

Sean Shrum
sean@shrum.net
www.shrum.net


mhx
Enthusiast / Moderator

Oct 22, 2001, 12:00 PM

Post #8 of 13 (4511 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

What do you mean? Sorry, but I don't get it... Crazy

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Oct 22, 2001, 12:39 PM

Post #9 of 13 (4509 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

Does CGI.pm have a preference as to what params to get?

You can call you shtml page directly and get the list of params supplied in the EXEC call, like:

http://myserver/ssi.shtml

You can also call the shtml page with a query string, like:

http://myserver/ssi.shtml?test1=one&test2=two

My question is: If you call the shtml page (containing a EXEC script call within) with a query string supplied in the page URL, what param listing (if not both) is prefered by CGI? Will the $q be a listing of the EXEC params, the query string params, or both?

Sean Shrum
sean@shrum.net
www.shrum.net


mhx
Enthusiast / Moderator

Oct 22, 2001, 9:01 PM

Post #10 of 13 (4505 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

Only the ones that are passed within the SSI command. To be honest, I'm not surprised because I've never seen a SHTML page called with a query string. But I guess that's due to my lack of knowledge when it comes to all that CGI stuff... Wink

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Oct 25, 2001, 10:35 PM

Post #11 of 13 (4500 views)
Re: &quot;use CGI&quot; and @ARGV; how to pass... [In reply to] Can't Post

I'm trying to understand the hash that gets created on new CGI.

Is it that CGI.pm is creating a hash in a hash whereas something like:


Code
my %errors = ( test => "This is a test", 
this => "This, that, and the other",
);

...is a hash of strings?

I am finding that if I want to modify a CGI created value, I have to go through this long syntax of:


Code
$input->param(-name=>'driver',-value=>$defaults{'driver'})

...instead of just doing:


Code
$input{'driver'} = $defaults{'driver'}

Thanks for all the help so far.

Sean Shrum
sean@shrum.net
www.shrum.net


S_Shrum
User

Nov 9, 2001, 1:17 AM

Post #12 of 13 (4488 views)
Re: &quot;use CGI&quot; and @ARGV; how to pass... [In reply to] Can't Post

Another question:


Code
if ( $input->param('sort') ) { ...

Does the above test for a defined 'sort' entry or a value other than null or blank ("")?

What I am thinking of:

I have a hash called defaults that I use in addition to my CGI $input hash. I want to list all the script parameters in this hash but for those entries that I do not want to set defaults for, I will set them to "". Like this:

Code
my Ūfaults = (	delimiter	=>	"|",		#required 
field => "", #required
file => "", #required
name => "", #required
order => "",
sort => "",
type => "",
);

...this makes it easier for people who use my scripts to know what parameters can be used and allows them to set defaults if they want easily.

I follow this up later with:


Code
# set defaults for parameters omitted by user 
for ( keys Ūfaults ) { $input->param(-name=>$_,-value=>$defaults{$_}) unless $input->param($_); }

...which sets any parameters that were not supplied by the user to the defaults in the hash.

If I do this though, will the IF call pass TRUE even though sort => ""? If so, what are my options around this?

TIA

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


S_Shrum
User

Nov 9, 2001, 1:43 AM

Post #13 of 13 (4483 views)
Re: "use CGI" and @ARGV; how to pass... [In reply to] Can't Post

Disregard...I decided to free up your time and just try it out...it seems to test for a value so setting any of the input entries to "' (blank) would return FALSE in a IF condition.

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives