user466534
user466534

Reputation:

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?

Upvotes: 1

Views: 13907

Answers (1)

simbabque
simbabque

Reputation: 54323

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.

Upvotes: 1

Related Questions