[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] STDIN problem



Frances,

I think this topic fits better in:
    macperl-webcgi@macperl.org
Maybe we can do future exchanges there?

In reply to:

>I am having trouble getting my script to read STDIN.  This code 
>works perfectly fine in Perl on UNIX, and I don't understand why my 
>MacPerl doesn't seem to understand STDIN.  I am using MacPerl 5 
>under WebStar v. 3.0.2.

1) The CGI spec is written specifically with Unix in mind; much of it 
makes no sense under MacOS.  For example, there is no such thing as 
STDIN in the MacOS.

2) Having said that, the "CGI Script" extension which is part of 
MacPerl does a wonderful job of making a MacPerl CGI work like a Unix 
CGI, including providing and populating the missing environmental 
variables and Standard In.

3) Nonetheless, much Web software cheats on the specs.  For example:

>while (<STDIN>) {
>  print "$_";
>}

... is not guaranteed to work according to my reading of the CGI 
spec.  It assumes that there is an EOF at the end of the form data, 
and the spec explicitly states that such is not guaranteed.  It may 
be that your Unix computer supplies one anyway and thus the above 
code would work there, but it is not legal CGI code.

>read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
>@pairs = split(/&/, $buffer);
>foreach $pair (@pairs) {
>    ($name, $value) = split(/=/, $pair);
>    $value =~ tr/+/ /;
>    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
>    $FORM{$name} = $value;
>}

This should work, and does when I insert it into a CGI of mine.

I am using Quid Pro Quo rather than WebSTAR, but I have successfully 
run code similar to the above under older versions of WebSTAR so I 
would not expect that to be the problem.  Make sure you have saved 
your MacPerl CGI using "Save As..." from the MacPerl File menu, 
selecting Type of "CGI Script".  My CGI, which works, is:

#!perl
#
# stdin_test.cgi
# Last Modified 04/14/2000
# by David Steffen
#
# Perl cgi Script to test contents of <STDIN>

# Standard http header used by all routines
$eol = "\015\012";                       # HTTP Standard
print "HTTP/1.0 200 OK$eol";
print "Server: Quid Pro Quo$eol";
print "MIME-Version: 1.0$eol";
print "Content-Type: text/html$eol$eol";

print "<html><head><title>Test of Standard In</title><head><body>$eol";

print "<h1>Test of Standard In</h1>$eol";
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);
     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $FORM{$name} = $value;
}
foreach $key (sort keys %FORM) {
     print "<P><B>$key</B> - $FORM{$key}</P>$eol";
}

print "<h1>Test of Environment</h1>$eol";
foreach $key (sort keys %ENV) {
     print "<P><B>$key</B> - $ENV{$key}</P>$eol";
}

print "</pre></body></html>$eol";

__END__

-David-
David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org