Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Perl setup on Windows
Perl setup on Windows
This tutorial describes how to setup Perl on Windows (tested with ActivePerl 5.8.8 on Windows XP Professional).

Install
Test Perl from the command line
Test the PPM (Programmer's Package Manager)
Test Perl with Apache (optional)
Test Perl with MySQL and/or PostgreSQL (optional)

Install top

Download the Windows MSI package of Perl (ActivePerl) from http://www.activestate.com/activeperl.
(You are not obliged to register, just click 'next' to download without register.)
Just double-click the downloaded .msi file and follow the Wizard instructions to install Perl.

Test Perl from the command line top

Start with a simple test, to be sure Perl is installed correctly.
Open a Command Prompt window, and invoke Perl from the command line:
perl -V
You should see a lot of info about your Perl.
Now test the full path to the Perl binary:
c:/Perl/bin/perl.exe -V
The output should be the same as in the previous example.

Now create a file called helloworld.pl which should contain this line:
print "Hello, World!\n";
Run from the command line (in the same directory where you created the file):
perl helloworld.pl
You should see the friendly output:
Hello, World!

Test the PPM (Programmer's Package Manager) top

Start the PPM from
Start -> All Programs -> ActivePerl -> Perl Package Manager
To see all installed Perl modules, query them all:
ppm> query *
To install a module, first search for it in the Active Repositories.
NOTE: In the PPM, module names use the Name-Subname syntax instead of Name::Subname.
I wanted to install Digest::MD5, so I typed:
ppm> install Digest-MD5
Error: Package 'Digest-MD5' not found. Please 'search' for it first.
OK, let's search:
ppm> search Digest-MD5
Searching in Active Repositories
  1. Digest-MD5-File    [0.04] Digest-MD5-File
  2. Digest-MD5-M4p     [0.01] Digest-MD5-M4p
  3. Digest-MD5-Reverse    [1] Digest-MD5-Reverse
'Digest-MD5' isn't available for ActivePerl on Windows, but 'Digest-MD5-File' will do the job.
As 'Digest-MD5-File' is listed first, you can install it either referring to its position in the search list (1) with
ppm> install 1
or using its complete name
ppm> install Digest-MD5-File
I prefer the latter one, to be sure I'm installing the right module.
A successful install should look like:
ppm> install Digest-MD5-File
====================
Install 'Digest-MD5-File' version 0.04 in ActivePerl 5.8.8.816.
====================
Downloaded 4647 bytes.
Extracting 5/5: blib/arch/auto/Digest/MD5/File/.exists
Installing C:\Perl\html\site\lib\Digest\MD5\File.html
Installing C:\Perl\site\lib\Digest\MD5\File.pm
Successfully installed Digest-MD5-File version 0.04 in ActivePerl 5.8.8.816.
ppm>
Read more about the PPM in the documentation that comes with ActivePerl.

Test Perl with Apache (optional) top

If you plan to use Perl together with Apache, then first setup Apache on Windows.
The Apache distribution already comes with a test script, printenv.pl, so if you installed Perl correctly, it should be sufficient to click
http://127.0.0.1/cgi-bin/printenv.pl
You should see some info about environment variables.

Put your script helloworld.pl in the Apache CGI folder (C:\Program Files\Apache Group\Apache2\cgi-bin by default on Windows) and try to click
http://127.0.0.1/cgi-bin/helloworld.pl
Disappointed?
Seeing

Internal Server Error

instead of the friendly 'Hello, World!' output?
Checking the Apache error log should always be the first place to debug (some output suppressed):
[error] [client 127.0.0.1] C:/Program Files/Apache Group/Apache2/cgi-bin/helloworld.pl is not executable; ensure interpreted scripts have "#!" first line
[error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Group/Apache2/cgi-bin/helloworld.pl
Compare the first line of printenv.pl and helloworld.pl:
#!c:/Perl/bin/perl.exe
is missing from helloworld.pl.
Add the line above, so helloworld.pl will now look like:
#!c:/Perl/bin/perl.exe
print "Hello, World!\n";
Try to click
http://127.0.0.1/cgi-bin/helloworld.pl
again.
It won't work, will still cause

Internal Server Error

What does Apache error log say (some output suppressed)?
[error] [client 127.0.0.1] malformed header from script. Bad header=Hello, World!: helloworld.pl
OK, CGI programming means that we have to send a HTTP header before any other output.
Just copy the following line from printenv.pl:
print "Content-type: text/plain; charset=iso-8859-1\n\n";
and add it as the second line to helloworld.pl, which will now look like:
#!c:/Perl/bin/perl.exe
print "Content-type: text/plain; charset=iso-8859-1\n\n";
print "Hello, World!\n";
Click
http://127.0.0.1/cgi-bin/helloworld.pl
and you should finally see the friendly output

Hello, World!

in your browser.

Test Perl with MySQL and/or PostgreSQL (optional) top


%%% TODO %%% Add PostgreSQL setup, DBD::mysqlPP DBD::PgPP setup

If you plan to use Perl together with MySQL, then first setup MySQL on Windows.
Install the DBD::mysql module using the PPM:
ppm> search DBD-mysql
Searching in Active Repositories
  1. DBD-mysql             [3.0002] DBD-mysql
  2. DBD-mysql-SimpleMySQL    [0.5] DBD-mysql-SimpleMySQL
  3. DBD-mysqlPP             [0.04] DBD-mysqlPP
ppm>
The DBI module will be installed (if not installed already) as a dependency of DBD-mysql.
ppm> install DBD-mysql
will cause a lot of output, but the two important lines are
...
Successfully installed DBI version 1.50 in ActivePerl 5.8.8.816.
...
Successfully installed DBD-mysql version 3.0002 in ActivePerl 5.8.8.816.
MySQL comes with the test database by default, which we will use in this example.
Create a user testuser with no global privilegies and with the password testpassword.
Add privilegies for testuser only for the database test, only on localhost.
Set the following database-specific privilegies: CREATE, DROP, SELECT, INSERT, UPDATE, DELETE.
(I use phpMyAdmin to set the MySQL privilegies.)

Then copy mysql-win.pl to the following path:
C:\Program Files\Apache Group\Apache2\cgi-bin\mysql.pl
mysql-win.pl is mostly copied from the example included in the DBD::mysql module:
#!c:/Perl/bin/perl.exe

use strict;
use DBI();

print "Content-type: text/plain; charset=iso-8859-1\n\n";

# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
       "testuser", "testpassword",
       {'RaiseError' => 1});

# Drop table 'foo'. This may fail, if 'foo' doesn't exist.
# Thus we put an eval around it.
print "DROP the foo table if it exists...\n";
eval { $dbh->do("DROP TABLE foo") };
print "Dropping foo failed: $@\n" if $@;

# Create a new table 'foo'. This must not fail, thus we don't
# catch errors.
$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");

print "INSERT 2 rows...\n";
# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Jan") . ")");

# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Berit");

print "INSERT a 3rd row...\n";
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 3, "JB");
print "SELECT the 3 rows...\n";
# Now retrieve data from the table (3 rows).
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();


print "UPDATE the 3rd row, from 'JB' to 'JanBerit'...\n";
$dbh->do("UPDATE foo SET name = 'JanBerit' WHERE name = 'JB' LIMIT 1");

print "SELECT the 3 rows...\n";
# Now retrieve data from the table (3 rows).
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();

print "DELETE the 3rd row...\n";
$dbh->do("DELETE FROM foo WHERE name = 'JanBerit' LIMIT 1");

print "SELECT the 2 rows...\n";
# Now retrieve data from the table (2 rows).
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();

# Disconnect from the database.
$dbh->disconnect();

Run
perl "c:\Program Files\Apache Group\Apache2\cgi-bin\mysql.pl"
from the command line.
The output should be
Content-type: text/plain; charset=iso-8859-1

DROP the foo table if it exists...
INSERT 2 rows...
INSERT a 3rd row...
SELECT the 3 rows...
Found a row: id = 1, name = Jan
Found a row: id = 2, name = Berit
Found a row: id = 3, name = JB
UPDATE the 3rd row, from 'JB' to 'JanBerit'...
SELECT the 3 rows...
Found a row: id = 1, name = Jan
Found a row: id = 2, name = Berit
Found a row: id = 3, name = JanBerit
DELETE the 3rd row...
SELECT the 2 rows...
Found a row: id = 1, name = Jan
Found a row: id = 2, name = Berit

You should be able to run the same script using Apache.
Just click http://127.0.0.1/cgi-bin/mysql.pl.
The output should be the same (except for the HTTP header, not seen in the browser).
Last modified: Wed Sep 7 08:26:13 EDT 2016