Just a guess, but perhaps it helps. I think I've encountered the same error when I was first running Perl/Apache under Windows. My problem was the shebang line in the cgi-script. Apache needs to know the exact location of your perl interpreter. For me, it was

C:\Programme\perl\bin\perl.exe

so I had to put

#!C:\Programme\perl\bin\perl.exe

as the first line of my cgi-script. Anything else doesn't work. While the script may run perfectly if it is started directly, it will not work when Apache is trying to start it. (This can be quite annoying if you develop a script using a Windows installation and upload the script on a UNIX server, because it just won't work unless you change the shebang-line...)

You wrote… Anything else doesn't work. While the script may run perfectly if it is started directly, it will not work when Apache is trying to start it.

What do you mean here?

You also wrote… (This can be quite annoying if you develop a script using a Windows installation and upload the script on a UNIX server, because it just won't work unless you change the shebang-line...)

How would I change the shebang line?

Also,

You wrote that my shebang line should look like this…

#!C:\Programme\perl\bin\perl.exe

My path is the same. However, I have a few questions.

1. Instead of “Programme” I have Program Files. How are spaces treated (the space between Program Files)? 2. Does the C: and Program Files have to be capitalized?

It doesn't matter if you take it out or leave it, at least on my machine.

>You wrote… >Anything else doesn't work. While the script may run perfectly if it is started directly, it will not work when Apache is trying to start it. >What do you mean here?

You can start (most) cgi-scripts from with the Perl interpreter directly from the command line. You don't get the HTML content displayed in your browser, but only printed in your command window. Under Windows, the shebang line is not of importance, except for the options you pass to the interpreter. Under Unix, it is used to determine the location of your interpreter. Apache also uses the line to determine the location of the interpreter under Windows.

> How would I change the shebang line?

I'm not quite sure what you want to know here ;-) You can just edit the line (?). Under Unix, the shebang line normally looks like

Code

#!/usr/local/bin/perl

Since I'm also testing my scripts under Windows, but my server is running Unix, I've written an upload script that not only uploads all new or modified files automatically, but also changes the shebang line of all cgi-scripts temporarily when uploading. I hope the answer you expected is somewhere in between...

> 1. Instead of “Programme” I have Program Files. How are spaces treated (the space between Program Files)?

That seems to be a problem. (I guess the german version of Windows has a slight advantage here...) I've tried to get my script running with spaces in the path, but it didn't work. I'd recommend that you install Perl in another folder, say 'c:\bin\', as I do when I'm working with the english version.

My main and most recent problem was my incorrect path in my shebang line, but it’s ok now. Thanks. Does my shebang line need to be like that for every script I have? Will my shebang line be different if I'm posting a .cgi script to a webhost’s server?

I have a few more important questions…

The script above was named simple.cgi

When I tested my script I was successful using the following url in both Netscape and IE… http://localhost/cgi-bin/simple.cgi

However, my test only worked for Netscape under this url… http://127.0.0.1/cgi-bin/simple.cgi

When I entered the above url in IE I received this message...

Connection refused Description: Connection refused

Is this a problem? Why did this happen only for the http://127.0.0.1/cgi-bin/simple.cgi address on Internet Explorer?

What the difference between the localhost and the 127.0.0.1 extension?

And finally…

What do I open when I want to test scripts directly in perl? Do I open (double click) perl.exe ?

On your machine, as long as the Perl interpreter does not move, yes. Except I would generally recommend to append a '-w' to the shebang line to make Perl issue some warnings. This generally leads to better Perl code and fewer / easier to find errors.

> Will my shebang line be different if I'm posting a .cgi script to a webhost’s server?

I think so. I've not seen a webserver yet having perl installed in 'c:/perl/bin/bin/perl'. Since most webservers I know about run Unix or Linux, your shebang line should most likely be

Code

#!/usr/local/bin/perl -w

But most webspace providers with cgi support list on their page where perl and sendmail are installed.

> When I tested my script I was successful using the following url in both Netscape and IE > http://localhost/cgi-bin/simple.cgi > However, my test only worked for Netscape under this url > http://127.0.0.1/cgi-bin/simple.cgi > When I entered the above url in IE I received this message... > Connection refused > Description: Connection refused > Is this a problem?

I don't think so. At least I don't think it's your problem. Perhaps it's your version of IE, although I really can't imagine why it would work the one way, but not the other. I've tried to reproduce this on my machine, but my IE (Version 5.00.2919.6307IC) worked with both URLs.

> Why did this happen only for the http://127.0.0.1/cgi-bin/simple.cgi address on Internet Explorer?

I can't tell you, really. Perhaps you can get some more information from Apache's server logs.

> What the difference between the localhost and the 127.0.0.1 extension?

There should be none. Here's what the Apache config file says:

# 127.0.0.1 is the TCP/IP local loop-back address, often named localhost. Your # machine always knows itself by this address. If you use Apache strictly for # local testing and development, you may use 127.0.0.1 as the server name.

BTW, there was another interesting thing in the config file:

# Apache parses all CGI scripts for the shebang line by default. # This comment line, the first line of the script, consists of the symbols # pound (#) and exclamation (!) followed by the path of the program that # can execute this specific script. For a perl script, with perl.exe in # the C:\Program Files\Perl directory, the shebang line should be:

#!c:/program files/perl/perl

# Note you _must_not_ indent the actual shebang line, and it must be the # first line of the file. Of course, CGI processing must be enabled by # the appropriate ScriptAlias or Options ExecCGI directives for the files # or directory in question.

I guess, reading the complete config file could be quite useful for me...

> And finally > What do I open when I want to test scripts directly in perl? > Do I open (double click) perl.exe ? No, you open a command prompt, change to the directory your scripts are in, and run the script with

Code

perl simple.cgi

> What would be the point of testing scripts directly in perl? Well, um, I can't see a point right now. When I started writing cgi scripts, I had quite a lot of experience writing Perl scripts. So I was more familiar with the command line execution of my scripts, and I've been struggling with Apache just as you did I only ran cgi scripts directly to trap errors. But there's a better way to do this. Just start every script you write with

Code

#!c:\programme\perl\bin\perl.exe -w use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use strict;

The CGI::Carp qw(fatalsToBrowser) will make all perl errors appear in your browser window. The CGI qw/:standard/ will be a great support when you want to generate HTML and parse CGI forms. One should really use this! The use strict is because I'm a Perl writer. Perl (and CGI) scripts tend to become more readable and error free when using this.

If there's anything else you'd like to know, or you would want in more detail, just ask.

Well, more or less yes. I strongly recommend 'use strict', because it forces a clean programming style. The two CGI modules are very useful for CGI programming. IMO far too less people are using it and mess around i.e. with parsing for CGI parameters. The shebang line is of course mandatory and must be the first. The others should appear somewhere on top of your script. If you include any other modules, put them before, somewhere in between, whatever you like. It's not of importance, as long as all this appears on top of your code.

> Is it because Program Files has a space in between it? > Do you know how I can get through this is DOS?

Yes, it is. You can change to this folder by typing

Code

cd "\program files"

Just quote any directory/file names with spaces in between when using the DOS prompt.

> 3. You quoted the httpd.conf with the following… > Apache parses all CGI scripts for the shebang line by default. > What does the above mean?

I just quoted this paragraph because it shows that the path in the shebang line may contain spaces. I don't know why this failed to work on my machine. The above just means that Apache looks at the shebang line to find out where the Perl interpreter is located.

I've tested your html/script on my machine. After applying two minor changes, they worked absolutely fine. These changes were: 1. The shebang line wasn't ok for my system 2. I had to change "emailtest.cgi" to "cgi-bin/emailtest.cgi" I think both changes don't have anything to do with your problem, but only with my configuration. Now, let's move on to analyze your problem. Did you ever get a cgi-script running with your current configuration? It seems that Apache is finding your cgi script, but doesn't know how to interpret it, so it just sends the script to the browser as plain ascii data. If you've been running scripts with this configuration, I really don't know why it doesn't work. If this is the first script you're trying to run, I guess it could be that your Apache httpd.conf is not properly set up. You should take a look at this file and see if the "ExecCGI" option is enabled and if there's a line

Code

AddHandler cgi-script .cgi .pl

I'm sorry I can't give you more precise information about this. Anyway, to convince you of using the CGI modules, I have rewritten the code you posted in a way that uses my first four lines and is - so I guess - more readable and understandable for the beginner. Although it doesn't use a single HTML tag, it contains both, the HTML and the CGI, in one file that is significantly shorter than the two original files. To make a transition easier, this script produces exactly the same output as yours, except it adds a 'Submit' button to the form.

Code

#!c:\programme\perl\bin\perl.exe -w use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use strict;

I just can’t seem to get this script working. You wrote…>If this is the first script you're trying to run, I guess it could be that your Apache >httpd.conf is not properly set up. >You should take a look at this file and see if the "ExecCGI" option is enabled and if >there's a line >

Code

>AddHandler cgi-script .cgi .pl

My httpd.conf file does have the ExecCGI file enabled, but I’ve never done anything with the…

Code

AddHandler cgi-script .cgi .pl

What is AddHandler? Is it necessary?

I heard when using windows all of my .cgi files should have the .pl extension instead. Is that correct?

Also, I tried running the script you posted, but I couldn’t. I named the file…

Code

mhx_email_test.cgi

…and I tried to open it in a browser (IE), but was unsuccessful. Nothing happens when I open it.

The AddHandler line tells Apache that files with .pl or .cgi extension are CGI-scripts. So Apache will add a handler for cgi-scripts. Well, I'm not sure if it's really neccessary. If I'd be running my own server, I'd probably know more about Apache ;-)

> I heard when using windows all of my .cgi files should have the .pl extension instead. Is that correct?

Well, I guess not. I think what you heard comes from that fact that the extension .pl is usually associated with Perl under Windows, while .cgi is not. You can configure the Windows version of Apache to use file association from the Windows registry. If you don't have the AddHandler line, then perhaps it will work only with the .pl extension. But, as I said before, I'm no Apache expert. I've installed it, configured it, and now only use it for offline testing of my less than 5 cgi-scripts... I think I'm more into Perl that into web programming. But I try to help where I can.

First, kill the 'use strict' line. This script wasn't supposed to run in 'strict' mode, because it doesn't declare its variables. (It's not yours originally, is it?) Most of the highlighted segments are uncritical. $ErrorPage and $ThankPage should both point to existing HTML files. @referers should contain the name of the server where the script resides. If you're testing locally, it's 'localhost'. $MailProgram is the most important thing. If you have sendmail installed (which I believe you don't have, I don't even know if there's a windows port of it), you must enter the path to sendmail here. For testing, you have to upload the script to your webspace provider and enter the path where he has installed sendmail. First of all, you should check if your provider supports sending mail.

Hi Marcus, To start, no this script isn’t mine. I got it off a link from this site.

In Reply To

You wrote… @referers should contain the name of the server where the script resides. If you're testing locally, it's 'localhost'.

So the segment that looks like this… # Script works only on your server(s) - ('URL1','URL2') @referers = ('www.yoursite.com','yoursite.com');

Should look like this? # Script works only on your server(s) - ('URL1','URL2') @referers = ('http://localhost','yoursite.com');

It’s just plain ‘ol localhost? Not localhost/cgi-bin/fileman.cgi ?

Why does the above say URL1, URL2?

In Reply To

You wrote…

$MailProgram is the most important thing. If you have sendmail installed (which I believe you don't have, I don't even know if there's a windows port of it), you must enter the path to sendmail here. For testing, you have to upload the script to your webspace provider and enter the path where he has installed sendmail. First of all, you should check if your provider supports sending mail.

I don’t have a host right now, that’s why I’m testing on Apache. What is sendmail? Do ALL emails sent from a Form require Sendmail?

I think it's just 'localhost'. But, anyway, I'm not sure if you will be able to get the script to work under Windows.

> I don’t have a host right now, that’s why I’m testing on Apache.

You should try to get a webspace provider. If you don't know any, try www.netfirms.com. Their service is free, and they offer CGI and sendmail support. That's how I'm testing scripts that use sendmail.

> What is sendmail?

sendmail is a (Unix) program to send e-mail. You just feed in a mail in ascii format and sendmail does all the rest.

> Do ALL emails sent from a Form require Sendmail?

Yes. Not actually sendmail, but at least another program that is able to send e-mail and can be controlled from perl. Here's a script I've assembled to test email sending at www.netfirms.com:

Code

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