Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

CGI with nested apps, each calling param() to get their args

by Zforgetaboutit (Initiate)
on Oct 11, 2012 at 21:55 UTC ( #998561=perlquestion: print w/ replies, xml ) Need Help??
Zforgetaboutit has asked for the wisdom of the Perl Monks concerning the following question:

$ perl --version
This is perl, v5.8.5 built for i386-linux-thread-multi

$ /usr/sbin/httpd -v
Server version: Apache/2.0.52 Server built: Jan 30 2007 09:56:53

$ cat /etc/redhat-release
Red Hat Enterprise Linux ES release 4 (Nahant Update 5)


From a Linux bash command line I invoke a1 with some command line args. a1 uses CGI param() to parse these args - no problems.

From the command line I invoke a2 with some command line args. a2 uses CGI param() to parse these args - no problems.

Change a1 so that it also invokes 'a2 with some command line args'. This instance of a2 can still parse args - no problems.

BUT invoked via a web browser a1 (still calls a2) can use param() to parse args but a2 seems to not be able to.

Here is a1.cgi

#!/usr/bin/perl -wl use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use diagnostics; my $q = new CGI; my $v1 = $q->param('v1') || ''; my $hdr = "I am a1.cgi:"; print $q->header; print "$hdr v1=$v1<br>"; my $cmd = './a2.cgi v2=dog 2>&1'; print "$hdr cmd = $cmd<br>"; my $cmdOut = qx[$cmd]; print "$hdr cmdOut = <br>$cmdOut<br>";

Here is a2.cgi

#!/usr/bin/perl -wl use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use diagnostics; my $q = new CGI; my $v2 = $q->param('v2') || ''; my $hdr = "&nbsp;&nbsp;I am a2.cgi:"; print "$hdr from param() v2=$v2<br>"; print "$hdr my cmd line args: @ARGV<br>";

Here is terminal session usage and output from a1.cgi # PASS

$ ./a1.cgi v1=box Content-Type: text/html; charset=ISO-8859-1 I am a1.cgi: v1=box<br> I am a1.cgi: cmd = ./a2.cgi v2=dog 2>&1<br> I am a1.cgi: cmdOut = <br>&nbsp;&nbsp;I am a2.cgi: from param() v2=dog +<br> &nbsp;&nbsp;I am a2.cgi: my cmd line args: v2=dog<br> <br>

Here is terminal session usage and output from a2.cgi # PASS

$ ./a2.cgi v2=dog 2>&1 &nbsp;&nbsp;I am a2.cgi: from param() v2=dog<br> &nbsp;&nbsp;I am a2.cgi: my cmd line args: v2=dog<br>

Here is browser output from web server url http://somewhere/a1.cgi?v1=box # FAIL

I am a1.cgi: v1=box I am a1.cgi: cmd = ./a2.cgi v2=dog 2>&1 I am a1.cgi: cmdOut = I am a2.cgi: from param() v2= I am a2.cgi: my cmd line args: v2=dog

As you can see, a1.cgi succeeds but a2.cgi fails to parse it's args via param(), when run by my 'simple' Apache web server.

I EXPECTED a2.cgi to sucessfully use param() regardless of my 3 invocation methods: 2 interactive + 1 apache.

This behavior is naively unexpected by me; can somebody please explain this mechanism?

Comment on CGI with nested apps, each calling param() to get their args
Select or Download Code
Re: CGI with nested apps, each calling param() to get their args
by Anonymous Monk on Oct 12, 2012 at 02:04 UTC

    This behavior is naively unexpected by me; can somebody please explain this mechanism?

    You're expecting too much from a debugging feature of CGI.pm

    The CGI protocol communicates through %ENV , and each child process gets the same %ENV as parent process, so to CGI.pm won't use the debugging freature and look at @ARGV

    But since CGI programs don't populate @ARGV you can use

    #!/usr/bin/perl -- use strict; use warnings; use CGI (); Main( @ARGV ); exit( 0 ); sub Main { my $cgi = @_ ? CGI->new(@_) : CGI->new; }

    Or it might be prudent to use GetOpt::Long / --query=a=1;b=2;c=3... instead of testing only for @ARGV

      Thank you very much for the simple & elegant solution to my problem. I understand your code.

      I'll be looking in the CGI man page to see where (the equivalent of?) this permitted type of arg-passing is mentioned since I totally missed it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998561]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-07-29 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls