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 4

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
Building Windows GUIs Using PerlNET
Windows GUI programming is one of the key benefits of PerlNET. Though there are other technologies that exist, like TCL/TK, to produce cross-platform GUIs using Perl, getting access to the .NET Framework's GUI classes is like opening a treasure vault! ActiveState provides a product that closely integrates with Visual Studio .NET called VisualPerl. However, drag-and-drop development is still yet to be realized. The code in Listing 13 shows a simple "Hello World" application.

This code should be compiled as:

plc --target=winexe HelloWindow.pl --reference=System.Windows.Forms.dll

Figure 1 shows the Windows form that displays when running HelloWindow.exe.

Figure 1: The Windows form created by PerlNET.
Note that the program is compiled with winexe as the target. This enables the compiled executable to run in its own thread. If I was to omit this option, compile and run the resulting executable from a DOS window, the DOS window would be locked until the HelloWindow program is closed. The necessary components (System.Windows.Forms.dll in this case) are attached to the HelloWindow.exe by the reference compiler option. The attribute [STAThread] used in line 10 ensures the compiled assembly runs in a Single Apartment Thread. Lines 3 through 5 provide the required namespaces to use the .NET Framework's Windows Forms classes. Line 9 indicates to the compiler that the HelloWindow package is inheriting from the Windows Forms class.

Line 16 instantiates the HelloWindow while the next line runs it using the static method Run of the Application class. Lines 23 and 24 set the text and size attributes of the form. Note that the attributes are not referred through the blessed hash but through a reference to the object itself.

Delegation, a key in the event-oriented GUI paradigm, is handled in PerlNET by add_ methods. These methods enable you to add your subroutines as event handlers. For example, the code for a MouseDown event would be:

=for interface . . . private void Form_MouseDown(any sender, MouseEventArgs evArgs); . . . =cut . . . sub _init { my ($this) = shift; $evtHandler = MouseEventHandler->new($this, "Forms_MouseDown"); $this->add_MouseDown($evtHandler); }

PerlNET provides an easy way to access all the properties and methods of an object without having to type the object name all the time. This is done by the with method. Using with, lines 23 and 24 could be rewritten as:



with ($this, Text=> "Simple Windows Forms", Size=> Size->new(int(300), int(200));

In this first article of a two-part series, I have demonstrated some of the powers of the Perl language. I highlighted features like economy of expressions, regular expressions, hashes, file inquiry operators and subroutines. I then explored the main agenda, which is Perl in .NET by providing a brief introduction to PerlNET and detailing how to create and consume .NET components using Perl. I then explored how to wrap existing Perl modules with a .NET assembly. Finally, I showed how to create a simple Windows program using Perl.

In the next article, I'll explore how to access databases using ADO.NET and Perl. In addition, I'll discuss how to create Web pages and Web services using Perl. Until then, happy programming!


Srinivasan Manickam has been in the IT industry for over 12 years. He has designed, developed, managed and mentored several projects, primarily under the Windows and Unix environments. His programming experience runs the gamut from Clipper to C# and from CP/M to Windows CE. Reach him at srinimanickam@india.com.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap