Click here to Skip to main content
14,868,503 members
Articles / Operating Systems / Windows
Posted 25 Nov 2006


14 bookmarked

Quick Introduction to PerlNET

Rate me:
Please Sign up or sign in to vote.
4.11/5 (7 votes)
4 Feb 2007CPOL3 min read
Introduction to ActiveState's PerlNET toolkit


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);

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.)


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


About the Author

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

Comments and Discussions

QuestionThe next step Pin
cdlvj29-Jan-16 4:54
Membercdlvj29-Jan-16 4:54 
QuestionURL? Pin
Uwe Keim26-Nov-06 19:18
sitebuilderUwe Keim26-Nov-06 19:18 
AnswerRe: URL? Pin
cycnus3-Dec-06 3:35
Membercycnus3-Dec-06 3:35 

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

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