PerlNET: The Camel Talks .NET - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

PerlNET: The Camel Talks .NET

Description:

PerlNET: The Camel Talks .NET. Jan Dubois. JanD_at_ActiveState.com. The Perl Conference 6 ... CLR Common Language Runtime. CTS Common Type System. CLS ... – PowerPoint PPT presentation

Number of Views:188
Avg rating:3.0/5.0
Slides: 27
Provided by: dickh8
Category:
Tags: net | perlnet | camel | talks

less

Transcript and Presenter's Notes

Title: PerlNET: The Camel Talks .NET


1
PerlNET The Camel Talks .NET
The Perl Conference 6
  • Jan Dubois
  • JanD_at_ActiveState.com

San Diego, July 26th 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
.NET Framework
Windows
Managed Runtime
Perl Host
Perl 5.6.1
Module Proxy
Module.pm
.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) _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)
11
Calling 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

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)(xpv4), 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() 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()

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 "_at_\n" if _at_ obj-foo() print "not
reached\n"
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 doesnt 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
  • mailtoJanD_at_ActiveState.com
Write a Comment
User Comments (0)
About PowerShow.com