Title: PerlNET: The Camel Talks .NET
1PerlNET The Camel Talks .NET
The Perl Conference 6
- Jan Dubois
- JanD_at_ActiveState.com
San Diego, July 26th 2002
2Agenda
- How does PerlNET work
- PerlNET Challenges
- Demonstrations
- Questions and Answers
3.NET Acronyms
- CLR Common Language Runtime
- CTS Common Type System
- CLS Common Language Spec
- CIL Common Intermediate Language
4.NET Terms
- Managed Code Data
- Types implemented in CIL
- Uses object references
- Garbage collection
- Unmanaged Code Data
- Platform native code (e.g. Win32, Kernel)
- Direct memory access
5PerlNET Design Goals
- Perl
- Standard Perl Syntax
- Full support for XS extension modules
- Reasonable execution speed
- .NET
- Using .NET objects from Perl
- Implementing .NET objects in Perl
- Cross-language inheritance support
- CLS compliance
6PerlNET Component
.NET Framework
Windows
Managed Runtime
Perl Host
Perl 5.6.1
Module Proxy
Module.pm
.NET Interface
7PerlNET Features
- Constructors Inheritance
- Methods incl. overloading
- Fields, Properties Indexers
- Events (Delegates)
- Enumerations
- Exceptions
- Custom attributes
- Namespaces
- P/Invoke
8Challenges
- Overloading
- Data Marshalling
- Object Lifetime
- Exception Handling
- Debugging
9Overloading
- .NET
- Statically typed
- Overloaded constructors methods
- Perl
- Dynamically typed
- Runtime parameter inspection
10Implementing overloaded methods
package PerlObj for interface void Write(char
arg) void Write(short arg) void Write(int
arg) void Write(long arg) void Write(double
arg) void Write(decimal arg) cut sub Write
my(this, arg) _at__ print arg
package PerlObj for interface string
Property private field string data cut sub
Property my(this, val) _at__ if
(defined val) this-data val
return this-data Called from
C obj.Property "My Value" Console.WriteLine(
obj.Property)
11Calling overloaded methods
- use PerlNET qw(AUTOCALL bool) Printsuse
namespace "System" call Write(System.Int32)m
y var 1Console-Write(var) 1 - call Write(System.Double)var
1Console-Write(var) 2 - call Write(System.Int32)Console-Write(int(var
)) 2 - call Write(System.String)Console-Write("var")
2 - call Write(System.Boolean)Console-Write(bool(
var)) True
12Passing .NET types to Perl
- Value types passed as bit patterns
- Wrapped in a Perl object
- Can be passed back to .NET as same type
- Overloaded operators
- Reference types are passed as GCHandles
- Reference counted on the Perl side
13Passing Perl variable to .NET
- All variables passed as IntPtrs
- PerlRT knows Perl variable layout
- Unsafe managed code accesses Perl variable
internals as needed - Data passed back to Perl is not unpacked
14Marshalling Strings
- System.String constructed directly
- from Perl scalar value
- Encoding enc (Flags sv_flags.UTF8) 0
- ? Encoding.Default
- Encoding.UTF8
- byte xpv (byte)(int)sv
- return new String((sbyte)(int)xpv,
- 0, (int)(xpv4), enc)
15Object Lifetime
- Perl
- Counts references
- Calls DESTROY() method immediately
- Deterministic destruction order
- .NET
- Tracks object references
- Calls Finalize() method later
- Managed objects destroyed in arbitrary order
- Cannot access other objects in Finalize()
16IDisposable interface
- Explicit Dispose() paradigmPerlObj obj new
PerlObj()try // Work with obj
obj.DoSomething()finally if (obj !
null) obj.Dispose() - C syntactic sugarusing (PerlObj obj new
PerlObj()) // Work with obj
obj.DoSomething()
17PerlNET object lifetime
- PerlNET makes sure that DESTROY is called
(eventually) - PerlNET objects are not collected by .NET
- unless they are explicitly disposed
- or at the end of the program
18Exception Handling
class CSharpObj static void Main() try
PerlObj.bar(new CSharpObj()) catch
(Exception e) Console.WriteLine(e)
void foo() Console.WriteLine("inside
foo") throw new ApplicationException()
package PerlObj for interface static void
bar(any obj) cut sub bar my obj shift
print "inside bar\n" eval obj-foo()
print "_at_\n" if _at_ obj-foo() print "not
reached\n"
19Debugging
- Perl debugger partially written in Perl
- Breakpoints freeze all managed threads
- Workaround run Perl in separate thread(uses
different call stack) - Step out doesnt work(need to set breakpoint
manually)
20Wrapping a CPAN Module
- Install CPAN module
- Create interface specification
- Compile into .NET assembly
- Use from C program
21Windows Forms Sample
- Create simple Form
- Add Textbox and Button
- Implement delegate for Click event
22Cross-Language Inheritance
- Design GUI using Visual C
- Add data members and virtual callbacks
- Implement callbacks in Perl
23ASP.NET
- Active Server Pages .NET
- HTML Source code
- Inherits System.Web.UI.Page behavior
- Compiles into .NET assembly
- Cached by IIS / ASP.NET
- Perl for ASP.NET
- CodeGenerator translates CodeDOM
- PerlNET creates .NET component
24Web Services
- Based on ASP.NET CodeGenerator
- Just add WebMethod custom attribute
- ASP.NET provides
- Interactive interface for testing
- WSDL description
25Summary
- PerlNET is part of the Perl Dev Kit
- Migrating Perl applications to .NET
- Reusing CPAN modules from other languages
- Native compiler to CIL for Perl 6?
26Questions Answers
- http//www.ActiveState.com/PDK
- mailtoJanD_at_ActiveState.com