To create a wrapper class, you create an interface based on the functionality of the Perl module rather than providing a complete implementation. The process of wrapping a module is like application design in reverse. Given an existing package, you must decide which methods and properties to expose or not to expose simply by including them inor excluding them fromthe interface. Before wrapping any module, be sure that it is installed on your system. Remember that when writing a PerlNET wrapper class, make sure you give it the same name as the Perl package you're wrapping.
Here's a simple module from CPAN, Text::Typoifier, which randomly creates errors in the text. This module has many procedures, but most of them are for inner use. To exploit the functionality of the module, you must be able to call the constructor, the transform method, and the errorRate property. Therefore, the wrapper component will have three entries in the interface definition:
static Typoifier Typoifier();
# transform method prototype
str transform(str text);
# errorRate property declaration
The Class constructor is always static (class method) and it must either return an object of the class you define or raise an exception. You may omit the return type for constructors. If you don't define a constructor, PerlNET will automatically generate the default constructor. After the interface definition, add the line to "require" the wrapped module itself. Compile this code into a DLL using plc.exe (PerlNET compiler) with the following command line (assuming that the source file name is Typoifier.pm):
plc -target= "library" Typoifier.pm
This command creates a typoifier.dll assembly, which can be referenced from .NET programs. The sample code for this article uses C# to create client programs that use PerlNET components.
Before presenting the C# client program for the Typoifier component, it's useful to look inside the new assembly (the dll) using the Intermediate Language Disassemblerildasm.exe. This tool is very useful for determining what classes expose what methods and properties inside the assembly. Figure 1 shows the internal structure of the Typoifier component.
|Figure 1: Looking inside the Typoifier component with ildasm.exe.
As you can see, the Text namespace exposes the Typoifier class. This class's methods and properties are exposed by the interfacethe class constructor, the transform method, and the errorRate property. PerlNET generated the special getter (get_errorRate) and setter (set_errorRate) methods for the errorRate property, and implicitly added other entries when building the assembly.
To test the PerlNET-generated assembly, build a simple .NET client program. Listing 1 shows a C# application using the Typoifier component to create sentences with typos according to a preset the errorRate. At this point, the fact that Typoifier was originally written in Perl is transparent to C#you can work with the class in the same way as with any other standard .NET framework class. If you're working in the Visual Studio IDE, create a C# project named TypoifierCli, add a reference to the Typoifier assembly, and then save and run the application. If you're working with the command-line compiler, you can compile the client program with the following command-line, which references the Typoifier assembly:
csc /reference:Typoifier.dll TypoifierCli.cs
The end result is an executable called TypoifierCli.exe. This program outputs ten lines with different type errors in the "perl" string.
PerlNET adds the Perl Interpreter component to every .NET assembly. Its function is to readdress the .NET calls in the client program to point to the corresponding subroutines inside the wrapped Perl module.