I am new to the Perl language, and I tried running it as I do for PHP files, by putting files in htdocs and then accessing them over localhost.
Below is the Perl file which I created, but wasn't able to run over localhost:
-----hello.pl---------------
#!/usr/bin/perl
print "Hello World.\n";
Install xampp. during installation, Make sure that, you have checked perl to be installed.
I assumed that, you have installed xampp in c:/xampp directory.
Now go to c:/xampp/htdocs directory. Inside htdocs directory create a directory perl. Now inside the perl directory, make a file named hello.cgi .
In hello.cgi write the following code snippet.
hello world program:
#!C:\xampp\perl\bin\perl.exe
# The above line is perl execution path in xampp
# The below line tells the browser, that this script will send html content.
# If you miss this line then it will show "malformed header from script" error.
print "Content-type: text/html\n\n";
print "Hello world."
Now start apache from xampp control panel. And in browser's url, enter localhost/perl/hello.cgi.
If your PHP install has the Perl module, you can evaluate Perl code directly from PHP.
<?php
print "Hello from PHP!";
$perl = new Perl();
$perl->require("test1.pl");
print "Bye!";
?>
First fix the "shebang" line to point to your Perl executable (I use WampDeveloper, not XAMPP, so your path will be different)...
#!C:/WampDeveloper/Tools/Perl/perl/bin/perl.exe
print "Hello World.\n";
Then create a "cgi-bin" directory inside the DocRoot and place you Perl script inside.
In this directory also create an .htaccess file with this inside...
DefaultType text/html
Options -Indexes +ExecCGI
SetHandler cgi-script
Go to the URL: http://www.example.com/cgi-bin/perlscript.pl
Note: This assumes the above directory does not have the htaccess option disabled for it in the main Apache configuration.
Please follow these steps:
Configure your web server to run Perl script (you may follow this url for more info http://editrocket.com/articles/perl_apache_windows.html).
Create your file (perl script ) and save it in your cgi-bin directory under root xampp.
(i.e : C:\xampp\cgi-bin).
N.B : your file should contain header info like
print "Content-type:text/html\r\n\r\n"; in top of script which will help browser to understand the type of information coming form web server.
Your script should have "shebang" line or else the server will throw an error.
Related
I am trying to make a simple program in perl. But whenever I try to run my file it always show the plain text instead of executing it.
Here is the code of my program I am working with:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n";
print "<title> PERL CGI</title>\n";
print "<body>";
print "hello perl-cgi!!!!!!!";
print "</body>";
print "</html>\n";
To run a '.pl or .pm' file in an Apache web server which has the compiler to execute, it should normally placed in a cgi-bin folder. Else you should add following content in your .htaccess file or in your Apache configuration file.
AddHandler cgi-script .cgi .pl .pm
Options +ExecCGI
After that you should give the file permission of that file as 755
You have to configure your web server to execute Perl scripts. There are several methods, the most simple is CGI, and the alternatives are mod_perl, FastCGI and PSGI. Configuration of each method depends on what HTTP server is used.
In case you use CGI in Apache2, make sure your script is executable (provided you use Linux/OSX/other UNIX: chmod +x hello.pl), and directory containing your script has Options +ExecCGI directive in Apache config file. Typically, /cgi-bin directory is set up correctly for CGI scripts.
Consult your HTTP server documentation on how to configure it to serve dynamic content.
It seems that you are trying to execute the CGI scripts. This CGI scripts can be a Perl Script, or a shell script, or C/C++ Program.
So You need to set up the HTTP server, so that whenever a file in a certain directory is requested, that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display. This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts.
Again Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is configured to handle CGI Programs. All the CGI Programs will be executed by the HTTP server if kept in a pre-configured directory. This directory is called CGI Directory and by convention it is named as /cgi-bin. By convention PERL CGI files will have extension as .cgi.
Here is a simple link which is linked to a CGI script called hello.cgi. This file is being kept in /cgi-bin/ directory and it has following content. Before running your CGI program make sure you have chage mode of file using chmod 755 hello.cgi UNIX command.
Code used for this hello.cgi is written below:
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<title>Hello Word - First CGI Program</title>';
print '</head>';
print '<body>';
print '<h2>Hello Word! This is my first CGI program</h2>';
print '</body>';
print '</html>';
1;
I am using xampp 1.8.1 on windows 7 and want to use it for perl, but when i execute even the most easy hello world perl script it gives me an error 500. Does anybody know what i am doing wrong? this is my 'hello world script:
#!/usr/bin/perl
print "Hello World.\n";
Thanks in advance,
Change the shebang line to actually point to your Perl path, for example:
#!c:/Strawberry/perl/bin/perl.exe
You can quote it if necessary:
#!"c:/Program Files/Perl/perl.exe"
Note that in Perl you can always use forward slashes for directories even on Windows (and this is preferred because it avoids messy escaping issues).
On Windows, the path in the shebang line is not normally used for execution. Thus the convention is often to use #!/usr/bin/perl for compatibility with Linux. However, Apache actually does use this path, so it needs to be set accordingly.
The correct code is:
#!C:\xampp\perl\bin\perl.exe
# The above line is perl execution path in xampp
# The below line tells the browser, that this script will send html content.
# If you miss this line then it will show "malformed header from script" error.
print "Content-ype: text/html\n\n";
print "Hello world."
In xampp the perl execution path is C:\xampp\perl\bin\perl.exe
Also, you can save a perl file ith extension .pl, .pm, .cgi . But for browser use, I would prefer .cgi extension.
I think this would help you.
I have a perl.cgi file which has the content:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>Hello World</h1>\n";
I made it executable. (chmod a+x perl.cgi)
Then I created a new file perl.htm in the same directory. Which has the following data:
Content-type: text/html
<p>RUN perl.cgi</p>
When I run the perl.htm in my browser then I get the output as:
Content-type: text/html
RUN perl.cgi
When I click on RUN perl.cgi another page opens and there the output is:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>Hello World</h1>\n";
i.e. the perl.cgi is not executing. Only the file contents are being shown.
EDIT: From the answers and comments I came to know that I will have to configure my web server (apache) to run cgi scripts. How can I do that? Let me know.
Sounds like Apache is not configured to run *.cgi files:
AddHandler cgi-script cgi pl
<Directory /path/to/cgi/files>
Options +ExecCGI
</Directory>
Make sure you are loading the CGI module in the httpd.conf file:
LoadModule cgi_module modules/mod_cgi.so or
LoadModule cgi_module modules/mod_cgid.so depending on which version of Apache you are running.
You can also read about additional solutions for
Dynamic Content with CGI.
Problem has been solved. I was not using httpd service at all, was opening from file:// URL.
Perl script is not executing in HTML form
You need to setup your web server so that it executes *.cgi files (or, more normally, all the files in a particular directory) rather than just delivering them to the browser as it does .html files.
How this is done depends on your server.
I try to execute a simple perl script on my server and I get an internal 500 server and when I check the error logs it shows:
Premature end of script headers: test.pl
Here is the perl script:
#!/usr/bin/perl -w
print "Content-type: text/plain\n\n";
print "testing...\n";
My cgi-bin folder has permissions of 0755. The script itself is also 0755. The script is owned by apache and its in the group apache. The script works fine via the command line.
What is the problem and how can I fix this?!
Thanks all for any help!
Update
Interesting find in suExec:
2010-09-14 17:38:28]: uid: (10001/som) gid: (2522/2522) cmd: test.pl
[2010-09-14 17:38:28]: target uid/gid (10001/2522 or 2521) mismatch with directory (48/0) or program (48/0)
But my cgi-folder is the same as the test.pl script - is it referring to another directory?
Plenty of good advice: How can I troubleshoot my Perl CGI script.
Update having seen your suexec error message: Looks like your server needs the CGI program to be owned by the same user as the directory. Try changing the ownership of the file.
There are a lot of good troubleshooting suggestions for Perl scripts giving that error message on PerlMonks: start here. I don't see any specific errors in your script, and it looks like you've covered the file permissions, so I'd start with the Apache configuration suggestions.
Use CGI module e.g.
use CGI qw/:standard/;
$q = CGI->new;
print $q->header('text/html');
print "testing...\n";
I am using a Perl module called from a CGI scipt in IIS 6 that is bombing. The identical folder structure on an XP machine (IIS 5.1) works fantastic. If I remove the module loading command at line 9, it will print "about to load" and "ok", but When I try to run
use Language::Guess;
I receive
The specified CGI application
misbehaved by not returning a complete
set of HTTP headers.
in the browser.
The folder structure is
/cgi-bin/test.pl
/PerlModules/Language/Guess.pm
I have tried adjusting the file/folder permissions and have reviewed my IIS configuration time and again. It runs fine from the command line on the IIS machine or if I copy the module into \Perl\site\lib, but I don't have permission to load modules on the shared server this script is destined for. Am I missing something simple?
Here is test.pl
use strict;
use CGI ':standard';
print header("text/html");
use lib "..\\PerlModules\\";
print "about to load<br/>";
#bombs here
use Language::Guess;
print "ok"
The problem is the line
use lib "..\\PerlModules\\";
Change it to the full path to where the Perl modules are:
use lib "C:\\Perl\\PerlModules\\";
or whatever.
Reason is that your CGI script run from the command line in the same directory is OK, but when it is being run with an absolute path by the server from a different directory, the directory ..\\PerlModules\\ is no longer the correct location of the modules (because now .. is relative to the server's directory, not your script's). When it tries to load the module, it can't find it and prints an error message. The web server can't cope with the error message, so you get the above.
If you don't want to use the absolute path in your cgi script, investigate using use lib inside a BEGIN block with the FindBin module.
One thing which alleviates this kind of problem is the CGI::Carp module and its "fatalsToBrowser" option:
use CGI::Carp 'fatalsToBrowser';
catches mistakes & throws them out to the browser. This is for debugging only though.