Login | Register   
LinkedIn
Google+
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
 

PerlNET: An Introduction : Page 3

Perl is a language that has been around for a while and is one of the most popular open source languages among system administrators, Web developers and the research community. Meanwhile, Microsoft's .NET technology, which is comprised of a framework and set of tools, was recently released for creating sophisticated applications. Is it possible to have any connection between these two different worlds? Yes it is! Perl is now a .NET language. This is the first of a two-part series written to introduce and explore the tools and technologies that are giving Perl and .NET a new dimension.


advertisement
Wrapping Existing Modules
In the following example, I wrap an existing Perl Module with PerlNET and use it from a C# program. With the wealth of available modules, this is an interesting application of PerlNET. Listing 11 shows the wrapper for a Perl Module available in CPAN.ORG, called Spell.pm. This module provides a method that returns a string that spells out the given integer. Listing 12 shows a simple C# program that utilizes this Perl Module, via the wrapper. You can easily start to appreciate how elegantly PerlNET achieves these integrations by noting how easy it is to wrap existing Perl Modules with PerlNET. You can download the Spell module (not the wrapper) from CPAN.ORG. If you have installed Perl, you can install this module without downloading by using the Perl Package Manger (ppm.bat). Refer to the Perl Package Manager's help information for further instructions on how to install Perl Modules.

There are certain key things to watch when wrapping existing Perl Modules with PerlNET. You have to name your PerlNET module with the same name as the Perl Module that you are wrapping. Also, there should be no methods with the same name as the module name. If there are, they will be considered as the constructors. And, finally, hashes are not allowed in .NET, so any hashes returned by subroutines should be converted to arrays and passed on to .NET. PerlNET provides a handy method modifier, wantarray!, that automatically converts a return list into a .NET compatible array. As an example, consider the following definition:

wantarray! Str() names();

It becomes easy to return a list from the name method as:

sub names { return qw(apples oranges bananas ); }

.NET Types
In .NET typed programs, PerlNET passes references to objects instead of hashes.
The key difference between Pure Types and .NET Types in PerlNET is that, in .NET Typed programs, PerlNET passes references to objects instead of hashes.

This includes even the constructor. As the constructor gets a reference to an already constructed object, it becomes an initializer rather than constructor. .NET Typed assemblies can also have Fields. The following code snippets show the key areas where this differs from Pure Typed assemblies:



Sub MyClass ## Constructor { #Get the reference to this object #Note:We get a reference though constructors #are static methods. my ($this, @args) = @_; } #Static method sub StaticMethod { #There is no reference to this object my (@args) = @_; } #Non Static method sub NonStaticMethod { #There is a reference to this object my ($this, @args) = @_; }

Mixed Types
Mixed Types differ from .NET Types only in the way that Mixed Types can store references inside a blessed hash. In all other aspects, Mixed Types behave the same way as the .NET Typed assemblies. This means that, in all the subroutines in the component, I get both a reference to the blessed hash and a reference to the object. Therefore, it is common to see the first line of such a method as:

my ($this, $self, @args) = @_;

In building components to inherit from, or be inherited by, other .NET components, you should use Mixed and .NET Types.

In summary, PerlNET handles type marshaling through conversion functions and =for interface POD comments. PerlNET components can interface with .NET in three different ways: as a Pure Perl component, as a .NET component and as a mixed component. In addition, you can wrap existing Perl Modules to produce a .NET assembly. I briefly explained when to use which type of interface. In the next section, I'll show how to develop Windows GUI programs using PerlNET.


Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap