|
|
||
|
||
>> Samples >>> Perl >>>> A Tour Of Using Perl On Windows |
|
|
ActivePerl by ActiveState made a well known port of perl to windows. ActivePerl can be used on Windows, Solaris and Linux. If you feel strong you can grab one of Borland C++, Microsoft Visual C++ or Mingw32 Libraries with GCC or EGCS compilers and build your own perl binary from source code. More information can be obtained by typing perldoc perlwin32 in your command shell (bash, command.com or whatever). If you are going to build your own binary or C modules you'll need a make tool. nmake can be found at Microsoft. Some of the interesting things in windows is that perl can use ADO databases, create OLE objects (control Microsoft Word for example), be a scripting language etc. Here is an example of controlling Microsoft Word by means of OLE Automation:
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
my $Word = Win32::OLE->new('Word.Application', 'Quit');
# $Word->{'Visible'} = 1; # if you want to see what's going on
$Word->Documents->Open("c:\\wordfiles\\file.doc")
or die("Can't access file ", Win32::OLE->LastError());
$Word->ActiveDocument->PrintOut({
'Background' => 0,
'Append' => 0,
'Range' => wdPrintAllDocument,
'Item' => wdPrintDocumentContent,
'Copies' => 1,
'PageType' => wdPrintAllPages,
});
You install ActivePerl by downloading it from ActiveState. There are versions for all Windows flavors. By default perl will be installed in c:\Perl. There are many ways to utilize perl on windows. You can use it to start programs from the command line (command.com or 4dos). You can run your CGI programs using web servers such as Apache, Xitami, IIS but probably the most important is Active Server Pages (ASP) scripting. The language used is called PerlScript and can be used instead of VBScript or as its complement. In fact at one time, ActivePerl was included by Microsoft in its Windows NT Resource Kit. For perl to work as CGI on Apache you need to have ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache/cgi-bin/" defined in your httpd.conf file. A simple CGI script would be:
#!/usr/bin/perl
use strict;
use CGI qw(:standard);
print header(),
"Hello, world";
You can find more instructions on this in some of the ActivePerl faqs (installed on your computer). Put only perl scripts in your cgi-bin folder nothing else. Active Server Pages Bundled with ActivePerl comes support for writing ASP pages using PerlScript. To be able to use PerlScript you need to know ASP object model and perl code that manipulates it. All of the standard ASP objects are supported (Request, Response, Session etc.). Here is how you use ADO to communicate to a database:
<%
# Create an instance of the ADO Connection object
$Conn = $Server->CreateObject("ADODB.Connection");
# Open a system DSN
$Conn->Open( "ADOSamples" );
# Execute an SQL Query
$RS = $Conn->Execute( "SELECT * FROM Orders" );
# Read a property to get the number of columns
# present in the Recordset returned from the
# query.
$count = $RS->Fields->{Count};
# Print out the names of each column
for ( $i = 0; $i < $count; $i++ ) {
$Response->Write( $RS->Fields($i)->Name );
$Response->Write("<br>");
};
# Loop the Recordset until there are no more records
#
while ( ! $RS->{EOF} ) {
for ( $i = 0; $i < $count; $i++ ) {
$Response->Write(" ");
$Response->Write($RS->Fields($i)->{Value});
$Response->Write("<br>");
};
# Move to the next record
$RS->MoveNext();
}
$RS->Close();
$Conn->Close();
%>
Perl for ISAPI is an extension made to run Perl scripts faster on ISAPI compliant Web servers. It Requires ActivePerl and an ISAPI compliant Web server (IIS). It is meant as a complement or a replacement of CGI protocol by using DLL calls to communicate. It is much faster than using perl.exe but since it shares memory with the web server it's much easier to crash it. With ActivePerl you can also develop Windows Scripting Components (WSC). WSC is formerly known as Scriplets and is a technology for developing Component Object Model (COM) components the easy way. An example of WSC would look like this:
<?xml version="1.0"?>
<component>
<registration>
description="Example"
progid="Example.WSC"
version="1.00"
classid="{74bb1ba9-2e69-4ad6-b02c-c52f3cbe153b}"
</registration>
<public>
<method name="SayHello">
</method>
</public>
<script language="PerlScript">
<![CDATA[
sub SayHello {
my($param) = shift @_;
return reverse($param);
}
]]>
</script>
</component>
As you can see this is an xml file. There is an xml declaration, registration, public and script elements. This component has to be registered by issuing regsvr32 c:\wsc\Example.wsc After you've done all this you can use it from ASP page with this code:
<%@Language=PerlScript%>
<%
my $obj = $Server->CreateObject('Example.WSC');
my $retval = $obj->SayHello("Hello World");
$Response->Write($retval);
%>
PerlScript can be used with Internet Information Server, Personal Web Server, Microsoft Internet Explorer (when installed on local machine) and Windows Scripting Host (WSH). You must be very carefull with WSH since some viruses are known to spread using it. WSH is meant to be a replacement of .bat (batch files) used in the past but with much more features. Here is how you would use PerlScript instead of Javascript in your browser (browser client evaluates this, nothing happens on the server side):
<HTML>
<HEAD>
<TITLE>PerlScript Hello World!</TITLE>
</HEAD>
<BODY>
<H1>PerlScript Hello world!</H1>
<SCRIPT LANGUAGE="PerlScript">
$window->document->write('Hello world!');
</SCRIPT>
</BODY>
</HTML>
PPM (Perl Package Management) is a tool used to install perl modules on windows just as you use CPAN module to do it on Linux. Unlike CPAN it uses ActiveState package repository. Murray Nesbitt, authored the module and made it part of ActivePerl distribution.
C:\W\Desktop>ppm
PPM interactive shell (2.1.2) - type 'help' for available commands.
PPM> help
Commands:
exit - leave the program.
help [command] - prints this screen, or help on 'command'.
install PACKAGES - installs specified PACKAGES.
quit - leave the program.
query [options] - query information about installed packages.
remove PACKAGES - removes the specified PACKAGES from the system.
search [options] - search information about available packages.
set [options] - set/display current options.
verify [options] - verifies current install is up to date.
version - displays PPM version number
PPM>
If you feel that you need perl support you can either:
Some of the code samples were take from ActiveState faq. That work was done by Henning Michael Moller-Nielsen of RTO based on examples by many people, especially Jan Dubois. Faq is maintained by Henning Michael Moller-Nielsen, Philip Martin, Kevin Meltzer and Eric Smith at perlwin32faq@rto.dk. Comments ? Further Reading:
< back |
|
Terms & Conditions Copyright © 2002 Mod Perl Development. All rights reserved. All Trademarks are property of their respective companies. |
|