Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Perl in C#

by fastofruto (Initiate)
on Feb 15, 2006 at 11:17 UTC ( #530344=perlquestion: print w/ replies, xml ) Need Help??
fastofruto has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i want to know if it's posible to embed perl code in C# applications or call perl functions from C#. thank you.

Comment on Perl in C#
Re: Perl in C#
by marto (Chancellor) on Feb 15, 2006 at 11:44 UTC
Re: Perl in C#
by turo (Friar) on Feb 15, 2006 at 12:08 UTC

    I've heard about .Net Perl compiler, so you can use perl code mixed onto your c# code ... how to use it? ...
    .... Its a mystery for me ... (I hope, not for much time)

    perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
Re: Perl in C#
by jonadab (Parson) on Feb 15, 2006 at 13:49 UTC

    This is really more of a C# question. It's possible to embed a perl interpreter in other code, but more likely what you want is to just shell out to perl, or use an equivalent for Inline (if C# has such a thing). I can tell you how to shell out to other languages from Perl, or just embed other languages Inline in Perl code, but if you need to know how to shell out to other languages (e.g., Perl) from C#, or embed other languages (e.g., Perl) in C# code, then I suspect you're going to have to ask a C# programmer.

Re: Perl in C#
by bmcnett (Novice) on Feb 16, 2006 at 07:15 UTC

    I've had some recent success using the Microsoft Script Control to host Perl inside my C# application.

    To embed a Perl interpreter in a C# program, add a reference to the COM object "Microsoft Script Control 1.0" and write code like this:


    MSScriptControl.ScriptControlClass Interpreter; Interpreter = new MSScriptControl.ScriptControlClass(); Interpreter.Language = @"PerlScript"; string Program = @"reverse 'abcde'"; string Results = (string)Interpreter.Eval(Program);

    The above is equivalent to the following Perl script, which embeds a Perl interpreter within a Perl interpreter:


    use Win32::OLE; my $Interpreter; $Interpreter = Win32::OLE->new('ScriptControl'); $Interpreter->{Language} = 'PerlScript'; my $Program = "reverse 'abcde'"; my $Results = $Interpreter->Eval($Program);

    This technique can also embed JScript in C#, VBScript in Perl, PerlScript in Visual Basic, etc. etc. The host can pass COM objects to the interpreter, through which the interpreter can call functions in the host.

    There's a newer C# interpreter-embedding system called "Visual Studio for Applications," or "VSA," but it's more complicated to use, and I'm not sure if it supports Perl yet.

    Another technology called "Windows Script Components," or "WSC," trivializes wrapping scripts in COM interfaces. Using WSC one could quickly write a library in Perl, and call it from C++ or C#.

    The library could later be rewritten in C++ or C# (or JScript or VBScript) without recompiling the applications that use it.

Re: Perl in C#
by Wyrdweaver (Sexton) on Apr 03, 2009 at 21:14 UTC
    Since the beginning of my experience writing in C#, I've also wanted to be able to re-use many of the great perl modules from within C# code. There are a ton of great algorithms already fully implemented on CPAN that could be put to good use in C#. Even just using simple modules like Getopt::Long and POD::Usage could make creating command line utilities so much easier.

    So, after quite a bit of slogging through Google research, looking at the various available possibilities, and reading these posts, I went ahead and designed an implementation for using arbitrary perl expressions within C#.

    I wrote a simple utility called NEW-GUID to serve as a fully implemented example of embedding perl in C#. It uses the PerlScript technique noted by bmcnett in his prior reply. It does double duty, also serving as an example using a replacement Win32 command line parser that I've written, Win32::CommandLine. So, I've included it in that module on CPAN (see Win32::CommandLine).

    Download it and look in the "extras" directory. The 'NEW-GUID.zip' file contains the C# source, a compiled executable, and a build script that should work on any Windows PC with Framework 2.0+ installed (that is any Vista+ OS or XP with dotnetfx v2.0+ installed). Right now, because of the PerlScript requirement, it requires an ActiveState perl installation, but it should be possible to refactor it into a form using just the perlXX.dll and a C# wrapper. (It might even be portable to Mono in that situation.)

    Full details on requirements and how to build the source are included in the README. All requirements are freely available and detailed instructions on how to obtain and install them are included in the README as well.

    Using the technique displayed in the code, you can run any arbitrary perl code, returning an arbitrary LIST of results (which can contain SCALARS and refs to SCALARS, ARRAYS, and/or HASHES to any arbitrary depth). The code to transfer any arbitrary structures across the COM/Active Script boundary is included and is fully implemented.

    Hope this helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://530344]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-07-12 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls