Presentation is loading. Please wait.

Presentation is loading. Please wait.

PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002.

Similar presentations


Presentation on theme: "PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002."— Presentation transcript:

1 PerlNET: The Camel Talks.NET Jan Dubois JanD@ActiveState.com The Perl Conference 6 San Diego, July 26 th 2002

2 Agenda  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

5 PerlNET 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

6 PerlNET Component Perl Host Perl 5.6.1 Module Proxy Managed Runtime Module.pm.NET FrameworkWindows.NET Interface

7 PerlNET Features  Constructors & Inheritance  Methods incl. overloading  Fields, Properties & Indexers  Events (Delegates)  Enumerations  Exceptions  Custom attributes  Namespaces  P/Invoke

8 Challenges  Overloading  Data Marshalling  Object Lifetime  Exception Handling  Debugging

9 Overloading .NET  Statically typed  Overloaded constructors & methods  Perl  Dynamically typed  Runtime parameter inspection

10 Implementing 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) = @_; print $arg; } package PerlObj; =for interface string Property; private field string data; =cut sub Property { my($this, $val) = @_; if (defined $val) { $this->{data} = $val; } return $this->{data}; } Called from C # : obj.Property = "My Value"; Console.WriteLine(obj.Property);

11 use PerlNET qw(AUTOCALL bool);Prints: use namespace "System"; # call Write(System.Int32) my $var = 1; Console->Write($var);=> 1 # call Write(System.Double) $var += 1; Console->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 Calling overloaded methods

12 Passing.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

13 Passing 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

14 Marshalling 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*)(xpv+4), enc);

15 Object 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()

16 IDisposable interface  Explicit Dispose() paradigm PerlObj obj = new PerlObj(); try { // Work with obj obj.DoSomething(); } finally { if (obj != null) obj.Dispose(); }  C # syntactic sugar using (PerlObj obj = new PerlObj()) { // Work with obj obj.DoSomething(); }

17 PerlNET 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

18 Exception 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 "$@\n" if $@; $obj->foo(); print "not reached\n"; }  inside foo   inside bar   inside foo   System.ApplicationException: Error in the application. at CSharpObj.foo()   System.ApplicationException: Error in the application. at PerlObj.bar(Object obj) at CSharpObj.Main() 

19 Debugging  Perl debugger partially written in Perl  Breakpoints freeze all managed threads  Workaround: run Perl in separate thread (uses different call stack)  “Step out” doesn’t work (need to set breakpoint manually)

20 Wrapping a CPAN Module  Install CPAN module  Create interface specification  Compile into.NET assembly  Use from C # program

21 Windows Forms Sample  Create simple Form  Add Textbox and Button  Implement delegate for Click event

22 Cross-Language Inheritance  Design GUI using Visual C#  Add data members and virtual callbacks  Implement callbacks in Perl

23 ASP.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

24 Web Services  Based on ASP.NET CodeGenerator  Just add [WebMethod] custom attribute  ASP.NET provides  Interactive interface for testing  WSDL description

25 Summary  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?

26 Questions & Answers http://www.ActiveState.com/PDK mailto:JanD@ActiveState.com


Download ppt "PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002."

Similar presentations


Ads by Google