Click here to Skip to main content
Click here to Skip to main content
Go to top

Quick Introduction to PerlNET

, 4 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Introduction to ActiveState's PerlNET toolkit

Introduction

PerlNET is a toolkit that makes it easy to re-use Perl modules from .NET. This toolkit also provides an extensive set of extensions (in Perl) to support the definition of CLR-based classes in a Perl syntax. This article will not focus on details involving PerlNET class definition capabilities, but rather demonstrate PerlNET's capability in providing an easy-to-cross bridge from .NET to Perl to promote quick reuse of Perl source.

Getting Started by Example

To demonstrate PerlNET's bridging capability, I have created a Perl module file for the express purpose of wrapping existing Geo-based Perl modules (all of which were obtained from CPAN) using the specialized PerlNET extension and syntax. The PerlNET documentation refers to these as aliases, and they are contained in the POD areas of the module file. They describe the types of properties, fields, return values of subroutines and methods as well as argument types. In the example below, I create a new module called GeoWrapper. This module will provide static methods to accept and return latitude and longitude coordinates or the component values of these data items.

The following code blocks represent the skeleton framework of a PerlNET assembly DLL wrapper. This includes the package name, import statements (use), the wrapper subroutines, and the PerlNET interface definition.

package GeoWrapper; 

use strict;
use diagnostics; 
use Geo::Coordinates::DecimalDegrees;
use Geo::Coordinates::UTM;
use Geo::Distance;

Below the wrapper subroutines are (manually) defined:

sub CalcDistanceInMiles
{
    my $lon1 = shift;
    my $lat1 = shift;
    my $lon2 = shift;
    my $lat2 = shift;
    my $geo = new Geo::Distance;
    return $geo->distance( mile => $lon1, $lat1, $lon2, $lat2 );
}

sub ConvertDMS2Decimal
{
...

sub ConvertLatLong2UTM
{
...

sub ConvertSec2HMS
{
...

To make these Perl subroutine's available in a PerlNET assembly DLL, a POD section is defined using special PerNET syntax that the compiler uses to define the assembly call entry points:

#----------------------------------------------------------------------- 
=for interface
     static double CalcDistanceInMiles(double, double, double, double);
     static double ConvertDMS2Decimal(double, double, double);
     static int[] ConvertLatLong2UTM(double, double);
     static int[] ConvertSec2HMS(int);
=cut 
#-----------------------------------------------------------------------

A module constructor is also defined:

sub new {
    my($package) = @_;
    my $self = bless {} => $package;
    return $self;
}

Syntax Notes (In Part From PerlNET's Docs)

  • All publicly callable (static) assembly methods must be declared in the interface definition. A method declaration consists of a list of method modifiers, followed by the return type, followed by the method name, followed by the parameter list declaration.
  • Method modifiers include access modifiers: public, protected, private or internal. They are mutually exclusive except for protected internal. The default access is public.
  • Another set of mutually exclusive modifiers consists of static, virtual and <override>. static indicates that this is a class method, virtual that the method can be overridden in a derived class, and override that a method already defined in the base class is overridden. In addition, the 'platform invoke' feature (P/invoke) uses the combination of static external to indicate a forwarder declaration to an unmanaged Win32 function in an external DLL.

Once the wrapper module has been defined, the PerlNET compilation tool (plc) is used to compile this source wrapper into a .NET callable, CLR-compliment assembly DLL.

Easy-To-Cross Bridge

As I have shown above, using PerlNET to bridge the world of .NET with Perl is relatively easy to do. PerlNET opens up the world of Perl, and CPAN in particular, to .NET. Check out and search CPAN here and then download a copy of PerlNET (part of the Perl Dev Kit from ActiveState, see this link for more information on PerlNET). Follow this example through and you can be calling Perl code from .NET in no time!

(Note, the .NET assembly DLL containing the Perl-based Geo utility routines is included and can be used as a standalone .NET library.)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kevin Wittmer
Web Developer
United States United States
. . . . . . . . . . . .

Comments and Discussions

 
QuestionURL? PinsitebuilderUwe Keim26-Nov-06 19:18 
AnswerRe: URL? Pinmembercycnus3-Dec-06 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 4 Feb 2007
Article Copyright 2006 by Kevin Wittmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid