forms, cgi - beginner question

J

John

I am trying to teach myself enough about cgi to make a
proof-of-concept demo.

Proposition is that I can use a Web form to collect certain data for a
front end-system.

Not looking for a lesson in HTML. I've written C programs that, in
turn, write HTML.

Not looking for lessons in Perl, I think. I've just written a batch
e-mail collector in Perl.

I am looking for a lesson in relating my html to my Perl code. (If
this makes my question off-topic for this group, please send me in the
right direction).

I have searched the FAQs (didn't read them cover to cover) and have not
found the answer I am looking for. So please, if you flame, rip me for
misreading the FAQs, not for ignoring them.

I am working from a book, "CGI Manual of Style," a Ziff-Davis number by
Robert McDaniel.

Page 8 says:
"...when a Web server receives a request for a file contained in in the
subdirectory cgi-bin, it knows that the file is a cgi script and will
run it and return only output from the script.

So here's my page:


<HTML><TITLE>Test page</TITLE>
<BODY>
<FORM METHOD=POST ACTION="/cgi-bin/example.pl">
Line:<INPUT TYPE=text NAME=namedtext VALUE="today" SIZE=30>
<BR>
<INPUT TYPE=SUBMIT VALUE=Send>
</FORM>
</BODY>


And here's example.pl:
#!/usr/bin/perl -w
print "This is example.pl speaking";


When I hit submit, the address in the browser window changes
to".../cgi-bin/example.pl"
But I do not get "only output from the script." I get the whole
shebang, as it were, with the word "print" etc. in my browser window.

So what am I doing wrong?

A vague question perhaps, but I could do with a pointer to some reading
material. What would help even more would be five minutes worth of
eye contact with someone who's done this before. There are only
thousands of you. But I don't have that, so I throw myself on the mercy
of this group.

I am trying to work the example using the Web server supplied with Mac
OS X,10.3.8.
But If I make the internal sale with the demo, the target system would
be an NT or Linux machine.
 
A

A. Sinan Unur

I am trying to teach myself enough about cgi to make a
proof-of-concept demo.
....

Page 8 says:
"...when a Web server receives a request for a file contained in in
the subdirectory cgi-bin, it knows that the file is a cgi script and
will run it and return only output from the script.
....

And here's example.pl:
#!/usr/bin/perl -w

use warnings;

is preferable to the -w switch because it allows you to selectively turn
warnings on and off.

Also, you are missing:

use strict;
print "This is example.pl speaking";


When I hit submit, the address in the browser window changes
to".../cgi-bin/example.pl"
But I do not get "only output from the script." I get the whole
shebang, as it were, with the word "print" etc. in my browser window.

That means your web server is not properly configured.

In addition, the script above is not a valid CGI script. You need to
send a content-type header.

#! /usr/bin/perl

$| = 1;

use CGI ();

my $cgi = CGI->new;
print $cgi->header('text/plain'), "This is example.pl speaking\n";
__END__
So what am I doing wrong?

A vague question perhaps, but I could do with a pointer to some
reading material.

perldoc CGI
What would help even more would be five minutes
worth of eye contact with someone who's done this before. There are
only thousands of you. But I don't have that, so I throw myself on the
mercy of this group.

No need to be overly dramatic. The CGI spec is available online. There
is a dedicated CGI newsgroup for CGI specific questions. Perl comes with
an excellent CGI module to make your life easier. Perl has a great FAQ
list:

perldoc -q CGI
I am trying to work the example using the Web server supplied with
Mac OS X,10.3.8.
But If I make the internal sale with the demo, the target system would
be an NT or Linux machine.

That is completely irrelevant.

Sinan.
 
I

Ian Wilson

John said:
I am trying to teach myself enough about cgi to make a
proof-of-concept demo.

Proposition is that I can use a Web form to collect certain data for a
front end-system.

Not looking for a lesson in HTML. I've written C programs that, in
turn, write HTML.

Not looking for lessons in Perl, I think. I've just written a batch
e-mail collector in Perl.

I am looking for a lesson in relating my html to my Perl code. (If
this makes my question off-topic for this group, please send me in the
right direction).

I have searched the FAQs (didn't read them cover to cover) and have not
found the answer I am looking for. So please, if you flame, rip me for
misreading the FAQs, not for ignoring them.

I am working from a book, "CGI Manual of Style," a Ziff-Davis number by
Robert McDaniel.

Page 8 says:
"...when a Web server receives a request for a file contained in in the
subdirectory cgi-bin, it knows that the file is a cgi script and will
run it and return only output from the script.

So here's my page:


<HTML><TITLE>Test page</TITLE>
<BODY>
<FORM METHOD=POST ACTION="/cgi-bin/example.pl">
Line:<INPUT TYPE=text NAME=namedtext VALUE="today" SIZE=30>
<BR>
<INPUT TYPE=SUBMIT VALUE=Send>
</FORM>
</BODY>


And here's example.pl:
#!/usr/bin/perl -w
print "This is example.pl speaking";


When I hit submit, the address in the browser window changes
to".../cgi-bin/example.pl"
But I do not get "only output from the script." I get the whole
shebang, as it were, with the word "print" etc. in my browser window.

So what am I doing wrong?

The common gateway interface (CGI) is a little more complex than you
assume. To use CGI it is easiest to "use CGI;"

The command "perldoc CGI" may help get you started.
 
J

John

I am trying to reconcile these two comments.

Is the web server irrelevant or is the web server the problem?
But I do not get "only output from the script." I get the whole
shebang, as it were, with the word "print" etc. in my browser
window.

That means your web server is not properly configured.
I am trying to work the example using the Web server supplied with
Mac OS X,10.3.8.
But If I make the internal sale with the demo, the target system would
be an NT or Linux machine.

That is completely irrelevant
 
J

John

The common gateway interface (CGI) is a little more complex than you
assume.

This might well be the answer to my question. Just not the answer I was
hoping for. I was looking for a proof of concept. The concept was that
this was doable with a certain amount of work.

So maybe it's not so doable. Knowing that serves my business purpose.
The command "perldoc CGI" may help get you started.

Maybe for some. For me, it's starting in the middle.
 
J

John

Copy and paste accident. Let's try this one again:

I am trying to reconcile these two comments.

Is the web server irrelevant or is the web server the problem?
 
G

Gunnar Hjalmarsson

John said:
And here's example.pl:
#!/usr/bin/perl -w
print "This is example.pl speaking";

When I hit submit, the address in the browser window changes
to".../cgi-bin/example.pl"
But I do not get "only output from the script." I get the whole
shebang, as it were, with the word "print" etc. in my browser window.

Try http://my.execpc.com/~keithp/bdlogcgi.htm
 
A

A. Sinan Unur

Copy and paste accident. Let's try this one again:

I am trying to reconcile these two comments.

Whose comments? Please make proper attributions when you quote someone
(in this case, me).
Is the web server irrelevant or is the web server the problem?

The details of your web server configuration are completely off-topic
here. However, if your web server is echoing the contents of a script
rather than running your script, then your web server is misconfigured.

These two statements have nothing whatsoever to do with each other. I do
not see the source of your confusion.

So, the first thing to do is to configure your web server correctly.
Second, you should look at the CGI spec to make sure the output your
programs produce conforms to the spec.

You have already stated that you don't need any Perl advice, but I will
point out that you should read

perldoc CGI

and

perldoc -q CGI

Sinan.
 
A

A. Sinan Unur

This might well be the answer to my question. Just not the answer I was
hoping for. I was looking for a proof of concept. The concept was that
this was doable with a certain amount of work.

Whatever "it" is, I assure you that it is doable. I have shown you how to
produce the output you expected from your script.
Maybe for some. For me, it's starting in the middle.

As I pointed out, the CGI spec is readily available on the WWW.

Since you know C and Perl, it should not be too hard to follow it.

Sinan.
 
S

Sherm Pendley

While the previous advice - use CGI.pm, warnings, and strict, read the FAQ,
etc. - is true, it doesn't really answer the question.
Page 8 says:
"...when a Web server receives a request for a file contained in in the
subdirectory cgi-bin, it knows that the file is a cgi script and will
run it and return only output from the script.

That's true in general, but be aware that cgi-bin is usually *not* a real
subdirectory that exists in your main document directory. It's usually
mapped to a different directory entirely.
I am trying to work the example using the Web server supplied with Mac
OS X,10.3.8.

Okay, that means most of your web content goes in:
/Library/WebServer/Documents

But, cgi-bin (in the URL) translates to this directory:
/Library/WebServer/CGI-Executables
But If I make the internal sale with the demo, the target system would
be an NT or Linux machine.

The URLs wouldn't change in that case, but the actual directories behind
them almost certainly would.

sherm--
 
A

Alan J. Flavell

That's true in general,

Excuse me stepping in here, but it's by no means "true in general",
rather, "it's a widespread convention".

(Of course, this is nothing specific to Perl. Such questions would be
better addressed on an appropriate web server configuration group.
Not that you need me to lecture you on that, but some readers...)
but be aware that cgi-bin is usually *not* a real subdirectory that
exists in your main document directory. It's usually mapped to a
different directory entirely.

Indeed.
 
H

hymie!

In our last episode, the evil Dr. Lacto had captured our hero,
I am working from a book, "CGI Manual of Style," a Ziff-Davis number by
Robert McDaniel.

Page 8 says:
"...when a Web server receives a request for a file contained in in the
subdirectory cgi-bin, it knows that the file is a cgi script and will
run it and return only output from the script.

Only if the web server is configured to recognize the cgi-bin
subdirectory as a place for CGI scripts. The directory can be named
anything. Or a web server can be configured without a cgi-bin directory.

There is nothing magic about the name "cgi-bin".

hymie! http://www.smart.net/~hymowitz (e-mail address removed)
===============================================================================
 
J

John

This is the kind of thing I was looking for originally. Somehow in the
meantime, I made things worse, and can no longer even echo the script
to the browser window.

Strictly speaking,no I did not know that cgi-bin is not a real
subdirectory. But based on the short experience with Personl Web
Sharing and plain old HTML, I was already dizzy trying to find the
'real' directory that apache/os x/apple/personal web sharing was
hiding. I knew it wasn't as simple as the folder in the finder being a
plain old directory.

Now I've gone from 'not found' to one of two results. In the Safari
browser, if I click Submit, I get jumped into a finder window, with the
icon of example.pl selected.

If I hit it from another machine in the room, running Netscape, I get
"Method Not Allowed"

I'm wondering, did I foul things up by *creating* a directory named
cgi-bin?

So I remove the directory blahblah/Sites/cgi-bin ...

example.pl is now only in /Library/WebServer/CGI-Executables

In Safari on the local machine, I'm now back to 'Not found.'

On the other machines, it now says "Forbidden - you do not have
permission to access /cgi-bin/example.pl on this server.

Back to /Library/WebServer/CGI-Executables, where we go 777 on the
permissions for example.pl

Over to Netscape.

Now we see 'Internal Server Error please contact the server
administrator.'

Contact the server administrator (me), who notes that Sinan thinks the
server administrator writes crappy Perl and can't configure a Web
server.

Could it be that my crappy Perl is crapping out?

This is indeed progress.

Following Sinan's advice, I ...

use CGI();

Voila!

Netscape now shows the output of the script, and only the output.

Thanks to all who contributed.

Now to all who suggested I was in the wrong group as this was really an
Apache question ...

You're right.

Here's why I did it anyway:

I know nothing of cgi, nothing about Apache and couldn't even frame the
question in those terms.

But I was hoping the common language of Perl would help me get the
question across. It did.

Thanks again.
 
A

A. Sinan Unur

Contact the server administrator (me), who notes that Sinan thinks the
server administrator writes crappy Perl and can't configure a Web
server.

Well, actually, I never said that. I said, if the web server is echoing the
contents of the script rather than running it, then, your web server was
not properly configured to serve CGI scripts out of that directory. I did
not know the peculiarities of your system, so I was not able provide the
insight Sherm provided.

Second, I did not say you write carppy Perl but pointed out that your
script did not send a proper header, so even if the server tried to run
your script, you would get an error, just as you described above.

I think you have come a long way realizing the distinctions between server
configuration, CGI, and Perl questions, and I am sure, in the future, you
will direct your questions to the most approriate forum.
Here's why I did it anyway:

I know nothing of cgi, nothing about Apache and couldn't even frame the
question in those terms.

If you are going to write CGI scripts, you probably want to learn something
about CGI. Being able to partition the problem domain will enable you to
pinpoint problems and solicit effective and helpful responses.

Sinan
 
S

Sherm Pendley

John said:
Now we see 'Internal Server Error please contact the server
administrator.'

Off-topic for this group, so I'll be brief.

Have a look at Console.app - you can use it to browse your server logs.
That's where Perl's error message will be when you get a "500 Server Error"
like the above. You can configure Console.app to sit quietly in the
background, and then unhide whenever something is added to the currently
open log - quite useful for error logs.

Also, have a look at the CGI::Carp module. You can use it to redirect errors
to the output that's sent to your browser, with this line:

use CGI::Carp qw(fatalsToBrowser);

sherm--
 
J

John

Partition the problem domain? When I started, I couldn't even find the
problem domain, so thanks for bearing with me.

Am I going to write CGI scripts? Maybe not. Ideally I'd like to
commission someone who's better at it. We had an exchange earlier in
this thread over the word 'doable.' You and I both know this is doable.
My job is to convince people in my organization that it's so doable
that it's the right tool for the job. That includes convincing my boss,
who might never have heard of Perl or CGI, and convincing a service
department that would rather not be bothered developing any. The latter
might easily convince the former that it's too much trouble and argue
for a competing tool (for example, human beings doing the job of
monkeys). However,if wifty ol' John has mangaged to cobble together a
form-finagling Web server in the basement on his day off, it's harder
to bellyache out of setting one up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top