Redirect Perl print statement to Apache log - perl

We have a Java web application running on Apache that calls Perl scripts in certain use cases. I would like to be able to redirect the print statements of the Perl scripts (which are printing to STDOUT by default) to the Apache log.
What is the best way to do this?

Using this in the Perl scripts worked:
print STDERR "my comment";

Related

Why is a Perl(.pl) file showing text instead of executing the file?

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;

xampp 1.8.1 perl not working and throwing error 500

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.

Can't get perl scripts to work (500 internal server error) on a tuxlite LAMP stack (Debian 6 Squeeze)

Does anyone who has experience with Tuxlite know how to get Perl scripts working? I've tried many, many things from tutorials on the web about getting Perl scripts to work with Apache without success. I keep getting a 500 internal server error. I'm allowing .cgi and .pl scripts in .htaccess.
My script is a hello world script
#!/usr/bin/perl print "Content-Type: text/html\n\n"; print "Hello World.";
I assume that this is not a apache/web problem.
Try your script on the shell and check the output.
(I assume that it does not run in the shell.)
May be your perl is not set up or a module is missing.
If that works, please add your script to your question.

How do I run my first Perl code on a Windows 7 system?

I have run this Perl code:
#!/usr/bin/perl
print "content-type: text/html \n\n";
print "Hello World.\n";
I have tried it in two ways, first one is Testing your Perl installation, but when I run by this way, it has some troubles, it asks me to choose a program with which I can run it, but no running yet.
Second way is first script with Padre, the Perl IDE, but when I write Perl code and try to save it, it does not show me Perl file's extension, so I can't save it as Perl file, so what could I do?
Your code looks like you want a CGI program. CGI means that you call your program through a web browser and get a website back. While vstm's comment was of course right for non-cgi programs, your example requires a little more stuff in order to work like that.
You will need to install a web server. Take a look at xampp. It is simple to install and maintain and comes with a mysql as well as an apache installation. I recommend the lite version since that does not have all the overhead.
Once you've installed it, you need to make some configuration so it can run your perl scripts. I take it you have already installed Active Perl. You then need to tweak the apache config.
in c:\xampp\apache\conf\httpd.conf you need to find the line that says
<Directory "C:/xampp/htdocs">
and read the comments (marked with #). You have to add ExecCGI inside the <Directory> section. Do that for every directory you want perl scripts to be run. Then look for a line that says
AddHandler cgi-script .cgi .pl .asp
and make sure it is not commented out.
Once you're done, place your program in the c:\xampp\htdocs folder (cgi-bin should also work) and change the shebang-line (the first line with #!) to where you've installed Active Perl, e.g. C:\perl\bin\perl.exe. It tells apache what program it should use to execute the perl script.
Also, add some more lines to your code:
#!C:\perl\bin\perl.exe
use strict;
use warnings;
use CGI;
use CGI::Carp('fatalsToBrowser');
print "Content-type: text/html \n\n";
print "Hello World.\n";
Now you need to run the apache web server. In the xampp installation dir there are several batch files that control apache and mysql. There's also a xampp-control.exe. Run it. In the new window, click on the Start button next to Apache.
In your browser, go to http://localhost/<yourscript.pl>. It should now say "Hello World!".
If it does not, make sure you're not running Skype. It blocks your port 80 which the apache tries to run on. You need to change apache's port to something else. Refer to this video.
A few words on the changes in the code I made and what they do:
use strict; should always be in your code. It forces you to honor certain guidelines and to write better code. This might seem strange in a Hello World program, but please do it anyway.
use warnings; tells you about things that might go wrong. Warnings are not errors but rather perl being helpful about stuff you might not know yourself. Use it.
use CGI makes the program's output go to the web server. You need that when you work with CGI programs.
print "Content-type: text/html \n\n"; is needed so the browser knows what to expect. In this case, an HTML website. It is called the HTTP-Header and contains a mime-type.
use CGI::Carp('fatalsToBrowser'); makes errors go to the browser. Without it, you'd never know about them unless you look in apache's error log.

Why doesn't my Perl CGI script work?

I really do not get how to run a Perl file. I have uploaded my .pl to the cgi-bin then chmod to 755. Then when i go to run the file i just get a 500 internal server error.
**/cgi-bin/helloworld.pl**
#!/usr/bin/perl
print 'hello world';
Any ideas as to what I am doing wrong?
Read the official Perl CGI FAQ.
That'll answer this, and many other questions you may have.
For example: "My CGI script runs from the command line but not the browser. (500 Server Error)"
Hope this helps!
You probably need something like
print "Content-type: text/html\n\n";
before your print statement. Take a look at http://httpd.apache.org/docs/2.0/howto/cgi.html#troubleshoot
It would help to know what server you are using, and the exact error message that's showing up in the server's logs. I'd guess that, if you are using Apache, you'll see something like "Premature end of script headers".
Look into using CGI::Carp to output fatal errors to the browser. use CGI::Carp qw(fatalsToBrowser);
Also, please definitely do use the CGI module to output any needed information such as headers/html/whatever. Printing it all is the wrong way to do it.
EDIT: You will also definitely be able to check an error log of some sort.
Perhaps you need my Troubleshooting Perl CGI scripts
First, find out the path to perl on that system and make sure the shebang line is correct. Giving more information about the system and the web server would also help others diagnose.
Then, try:
#!/path/to/perl/binary
use strict;
use warnings;
$| = 1;
use CGI qw( :default );
print header('text/plain'), "Hello World\n";
Make sure that you can run the script from a shell prompt, without invoking it through Perl. In other words, you should be able to go to your cgi-bin directory and type:
./helloworld.pl
and get output. If that doesn't work, fix that. In looking at the output, the first line must be:
Content-Type: text/html
(Or text/plain or some other valid MIME type.)
If that's not the case, fix that.
Then you must have an empty line before the body of your page is printed. If there's no empty line, your script won't work as a CGI script. So your total output should look like this:
Content-Type: text/html
hello world
If you can run your script and that's the output, then there's something weird going on. If Apache is not logging the error to an error_log file somewhere, then maybe there's some problem with it.
Did you enable Apache to server .pl files as CGI scripts? Check your Apache config file, or (quick but not guaranteed test) try changing the file extension to .cgi. Also, make sure your shebang line (#!) is at the very top. Finally, check the line endings are Unix if your server is Linux. And yes, test it from the command-line, and use strict; for better feedback on potential errors.