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
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:
. . .
private void Form_MouseDown(any sender,
. . .
. . .
my ($this) = shift;
$evtHandler = MouseEventHandler->new($this,
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!