Installtop
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 linetop
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:
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.exeusestrict;
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();
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).