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>';
On Mon, Apr 11, 2011 at 04:05:31AM -0700, prashant kaushal wrote: > Hi Di,
> Yesterday i tried a "Hello world" program in perl using cgi script on a windows platform. The steps i followed were:
> 1. Made a directory in "C:" and named it "cgi-bin" > 2. Wrote my source code file and saved it with "test.cgi"
That won't do, you need to configure a webserver to execute that code. There are 2 ways to go about it:
1: install Apache, you can find a ready-to-go distribution at http://apachefriends.org 2: configure IIS to execute perl CGI, which is not extremely difficult, but requires you to know how to create a website on IIS. Also, the top results on that search sadly still refer to ActiveState, so are not relevant anymore.
In a sense, what you need to do is add a .pl/.cgi association to IIS and configure it to open these files with Strawberry/perl/bin/perl.exe
If no further information will be given here by the time I get home (to a windows machine), I'll post a more thorough guide.
La perfection soit atteinte non quand il n'ya plus rien à ajouter, mais quand il n'ya plus rien à retrancher.
> but now instead of giving the output it just shows the original source code. > please help! > regards
> PS: software that I've installed on my pc is "Strawberry perl plus padre"
Fist of all, either change the line
to reflect the true path to the perl compiler on your machine. Either that or change the file type to .pl and make the association with the compiler.
Secondly, you don't need to print "\r\n" as line terminators. This will only do what you want if you 'binmode' the output stream; otherwise "\n" will be output as CR LF, and adding a preceding "\r" will produce CR CR LF, which doesn't comply with HTTP standards.
Now, you are opening a Perl program file in a web browser. The browser's job is to interpret what it receives (whether from a local file or a remote HTTP server) and present it on screen. It expects HTML data (or perhaps XML) and does its best with any other content by presenting the data just as it is. There is never any possibility that the browser should know to /execute/ a Perl program if it sees one; indeed any CGI code must be run on the server so that it has access to the server's environment: files, databases etc.
So it is the server's job to run your Perl program and pass its output to the browser for display. But you have no web server - all you have is a file, so the browser displays the data from that file.
You must install and configure an HTTP server. The configuration will determine whether the contents of a given file should be served unmodified, or be run as a program and the output delivered instead. It is quite possible to run a server process on the same machine as where the file and web browser exist, but expect to do a lot of reading to understand the details of the configuration.
On Windows you should install the 'World Wide Web Services' features. Unfortunately this is not the forum to help you further with that.
> please suggest the steps to configure my web server.
It depends on which web server you are using. You will probably find a cgi-bin directory (in Debian/Ubuntu, Apache's cgi-bin is at /usr/lib/cgi-bin/) which is configured to serve CGI scripts. Else, you will have to configure the web server to allow another directory to serve CGI scripts and copy the script there.
Regards, Alan Haggai Alavi. -- The difference makes the difference
If you are using a Windows System, you can go into the Control Panel, click "Enable or Disable Windows Components" and select IIS. Depending on your version of Windows, you should either have IIS 6 / 7, probably IIS7 if you are using Windows 7 as well. After selecting it, let Windows Install it, then reboot your computer, use this (http://goo.gl/Ale0z) to download and set up Perl on IIS. Bind the webserver to your internal IP address, because so far you probably do not need a publicly available website. Then, put the *.cgi / *.pl file in your website root, usually C:\inetpub\wwwroot\, and access it with your browser. It took me a few days of tinkering to get it up and working properly, but this should suffice for now.
Otherwise, you can download Apache webserver, which comes with perl, but the instructions for setting that up is different depending on your OS.