0

I'm a newbie with Perl. I've created a simple file test.pl with the code:

#!/usr/bin/perl
print 'test';

uploaded it to my shared hosting with Linux where PERL is enabled and changed my PERL file permissions to 755 via cPanel file manager.

But when I follow by the link like mydomain.com/test.pl, I receive the "500 Internal Server Error".

In the "Errors" section of my cPanel I see just the line

uid: (1639/uahst341) gid: (1641/uahst341) cmd: test.pl

where uahst341 is the current user of cPanel

I don't have the access to the command line. Can I and how to run the file without the command line prompt?

UPDATED

With the help of my hosting provider I found out that I must to create PERL file inside of /cgi-bin folder, type a code via cPanel File manager and add the line like print "Content-type: text/html\n\n"; However, when I write the same code in Notepad++ in UTF-8 and then upload the file to the server, I again receive the 500 error and see that uploaded file has 2 bytes more than created via File manager (in UTF-8). So there is a problem with the encoding. Which exactly?

sprsr
  • 229
  • 1
  • 2
  • 12
  • 1
    What is your goal here? Are you trying to create a Perl-based website (CGI), or are you trying to learn Perl in general? – u1686_grawity Jun 22 '21 at 12:42
  • I just need an environment to test some code in PERL with MySQL – sprsr Jun 22 '21 at 12:43
  • Have you considered installing Perl on your own computer? Does your cPanel have any kind of "error log" or "CGI log"? – u1686_grawity Jun 22 '21 at 12:46
  • The only "error log" I found is the Errors section of cPanel I mentioned in the question – sprsr Jun 22 '21 at 12:48
  • 1
    Are the line endings set to \r\n in Notepad++? – Andrew Morton Jun 22 '21 at 14:16
  • Yes, I've replaced \r\n\ with \n and resolved the problem, thanks – sprsr Jun 22 '21 at 14:27
  • Your extra 2 characters might be a byte order mark (BOM), and I wouldn't be surprised if that disrupted the shells ability to parse the `#!` line at the beginning, so it never gets to perl. – mc0e Jul 02 '21 at 14:56
  • If your file has a BOM, this will show you how to remove it: https://stackoverflow.com/a/20617241/2109800 – mc0e Jul 02 '21 at 15:39

1 Answers1

3

Perhaps your file is being run, and the problem is that it doesn't output a valid HTTP response? I'd fix that first, though I'm not sure if the file is being run or not.

This is close to the minimal response:

#!/usr/bin/env perl
print "HTTP/1.1 200 OK\r\n";
print "Content-Type: text/plain\r\n";
print "\r\n";
print "test\r\n";

Use of env in the first line is optional, but makes your code more general and is a good habit.

You can usually count on your web server to do a few bits for you, so a slightly simpler version would be:

#!/usr/bin/env perl
print "Content-Type: text/plain\n\n";
print "test\n";

It is good to start using suitable libraries as soon as you are ready for them. Have a look at https://perlmaven.com/hello-world-with-plain-cgi for some sample code. If you've done a bit in other languages and are familiar with object oriented concepts, then you should probably start with CGI::Simple in perl.

If your perl script isn't being executed at all, then you need to know a bit about how the web server is configured. The web server (e.g. it might be apache or nginx) needs to see the .pl suffix and know to run the file with perl. It might or might not be configured that way. It might be that it wants you to use a .cgi suffix, or you might need to put the file in a /cgi-bin/ directory at the top of your site. If your not sure, then ask your hosting provider.

mc0e
  • 389
  • 2
  • 7
  • Regarding e.g. `Content-Type: text/plain\n\n`, for basic [CGI](https://en.wikipedia.org/wiki/Common_Gateway_Interface) scripts, some variation of this line has to be sent regardless of language (so in e.g. C++ or Python as well). Also, another common type to return is `Content-type: text/html` for HTML content. – Anaksunaman Jun 23 '21 at 15:46