Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Make Your Existing Perl Apps .NET-compliant

Learn how CPAN Perl modules can be made automatically available to the .NET framework. The technique involves providing small PerlNET mediators between Perl and .NET and knowing when, where, and how to modify.


advertisement
The Perl language gains popularity daily among programmers from all over the world because of its flexibility, vast sphere of application, and easy source-code availability. The Comprehensive Perl Archive Network (CPAN) distributes Perl modules as open-source software. You can download the packages from CPAN using the ppm tool that comes with the ActivePerl distribution. ActiveState's latest release of PerlNET, included in the Perl Dev Kit, brings Perl programming into the realm of .NET compliant languages, which may increase Perl usage substantially.

PerlNET allows you to create .NET-compliant applications and components using the Perl language. One key feature is that you can use existing Perl packages (modules) from CPAN within the .NET environment and in your favorite .NET compliant language without rewriting the Perl code. Instead, you create a simple wrapper class in PerlNET for the package, and enumerate all the module methods or properties you want to expose to .NET. In other words, you need to provide an interface definition through which NET programs will interact with the Perl module. PerlNET introduces new Plain Old Documentation (POD) blocks for this purpose, using the syntax:

=for interface . . . =cut

Inside these blocks, you define the prototypes of methods and declare properties. You can separate the definitions into several blocks or unify them under a single block. When providing an interface definition for Core Perl modules, include the [interface: pure] attribute inside one of the POD blocks (usually the first). This instructs PerlNET to create a .NET component in the Core Perl manner:


=for interface [interface: pure] . . . =cut

Because .NET is a strongly typed environment, you must provide the types for methods arguments, for methods return values, and for properties when defining the interface. For example, if some subroutine Foo accepts a string and returns an integer, it should have the following prototype in the =for interface block:

=for interface int Foo(str text); =cut

Table 1 shows .NET types from the System namespace and the corresponding PerlNET types.

.NET Type

PerlNET type

Boolean

bool

Char

char

Sbyte

sbyte

Int16

short

Int32

int

Int64

long

Byte

byte

UInt16

ushort

UInt32

uint

UInt64

ulong

Single

float

Double

double or num

Decimal

decimal

Object

any

String

str

The type system in PerlNET is almost identical to that of the .NET C# language. There are two exceptions: str and any types are used for the System.String and System.Object classes respectively (C# uses string and object aliases for them). You may also use both the num and the double types for denoting the System.Double value type.

Every non-static method of the Core Perl module expects a reference to a hash as a first argument. This hash may be also referred to as a package. It stores values of properties for the current instance. Its reference plays the same role as the this pointer in C++ or Me in VB. PerlNET automatically passes a reference to a package as a first argument to all non-static methods, and you should not list it in the arguments list.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap