beng has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

Need help in solving the following problem :

I have an apache web server running with perl 5.8.4 on a Linux Redhat 9.0 box. In the cgi-bin folder, i have this file test.pl with the following content :
#!/usr/local/bin/perl print "Content-type: text/html\n\n"; print "<H1>Hello World</H1>\n";
Just a simple script to help illustrate the problem I'm facing.

When I run this from the browser, it returns an INTERNAL SERVER PROBLEM. Log from apache shows "Premature end of script headers: test.pl".

Now... I have searched thru this site and other PERL sites, and gone thru all the steps the others have suggested, but still things didn't help.

The following are some further testing done by me :

- If I run the script directly using "./test.pl", it gives me the following result :
[root@www cgi-bin]# ./test.pl : bad interpreter: No such file or directory


- If I run the script using "perl test.pl", it gives me the correct output :
[root@www cgi-bin]# perl test.pl Content-type: text/html <H1>Hello World</H1>
I checked the path of PERL and that's correctly entered. So couldn't be any path issues.

- If I edit the script to add in a "-w" after the "perl" at the first line, everything runs fine, even from the browser. Here's the updated script :
#!/usr/local/bin/perl -w print "Content-type: text/html\n\n"; print "<H1>Hello World</H1>\n";
Is there anything that I have not done correctly? Why is it that I require a "-w" in this server, while not in any of the rest of my apache servers with PERL??

Anybody can shed some light into me?

Replies are listed 'Best First'.
Re: bad interpreter: No such file or directory
by gellyfish (Monsignor) on Jul 22, 2004 at 08:17 UTC

    There is a character after the 'perl' (probably a DOS line ending) - if you are using FTP to transfer the file from a windows machine to a Unix one you should use ASCII mode

    /J\

      Yup, this is what the problem is. The #! line is most likely "/usr/bin/perl\r\n", and the carriage return is putting the cursor back to the beginning of the line (which makes the shell's error message apparently start over with no path). Whenever you get strage error messages like this using od or your platform's variant of strace can be enlightening in seeing exactly what's being output or exec'd.

Re: bad interpreter: No such file or directory
by atcroft (Abbot) on Jul 22, 2004 at 08:15 UTC

    Is the perl interpreter located at /usr/local/bin/perl, or /usr/bin/perl, or another location? When you type perl test.pl, you are using the perl interpreter as found in your path; when you type ./test.pl, it is as if you had typed /usr/local/bin/perl test.pl. Depending on the system, one way you might be able to determine the location of your perl interpreter would be to type which perl, which will show you the first location for the perl interpreter as found in your path.

    Hope that helps.

Re: bad interpreter: No such file or directory
by trantor (Chaplain) on Jul 22, 2004 at 08:48 UTC

    If this is a standard Red Hat Linux 9, then the correct path is /usr/bin/perl and your script should start with:

    #!/usr/bin/perl.

    If in doubt, type this command at the shell prompt, it will tell you where the perl executable is found:

    which perl

    As a side note, "Perl" is the language, "perl" is the program used to run Perl programs, but there is no such thing as "PERL". Please check the FAQ here.

Re: bad interpreter: No such file or directory
by Anonymous Monk on Jun 07, 2013 at 16:05 UTC
    Hi, try to do the following and see if its working? I may be wrong? perl /file_path/filename.pl and see if it works.