Beta

Slashdot: News for Nerds

×

Welcome to the Slashdot Beta site -- learn more here. Use the link in the footer or click here to return to the Classic version of Slashdot.

Thank you!

Before you choose to head back to the Classic look of the site, we'd appreciate it if you share your thoughts on the Beta; your feedback is what drives our ongoing development.

Beta is different and we value you taking the time to try it out. Please take a look at the changes we've made in Beta and  learn more about it. Thanks for reading, and for making the site better!

Sickness

rjw57 (532004) writes | more than 10 years ago

User Journal

Well I've been playing with embedding Perl within C# and have managed to write the first 'useful' program. The following is a re-write of the LWP simple test program in C#.

namespace PerlEmbedExamples {
using System;
using Perl;

Well I've been playing with embedding Perl within C# and have managed to write the first 'useful' program. The following is a re-write of the LWP simple test program in C#.

namespace PerlEmbedExamples {
    using System;
    using Perl;

    class LWPExample {
        public static void Main(string[] args) {
            Console.WriteLine("Embedding Perl within Mono - LWP Example");
            Console.WriteLine("(C) 2004 Rich Wareham <richwareham@users.sourceforge.net>\n");

            Interpreter interpreter = new Perl.Interpreter();
            interpreter.Embed();

            try {
                interpreter.Require("LWP::UserAgent");
                Scalar ua = interpreter.CallClassMethod("LWP::UserAgent", "new");
                interpreter.CallMethod(ua, "agent", "Mozilla/6.0");
                Scalar req = interpreter.CallClassMethod("HTTP::Request", "new",
                        "GET", "http://example.com/");
                Scalar res = interpreter.CallMethod(ua, "request", req);

                if(interpreter.CallMethod(res, "is_success")) {
                    Scalar content = interpreter.CallMethod(res, "content");

                    Console.WriteLine("Success!");
                    Console.WriteLine("Content:\n{0}", content);
                } else {
                    Console.WriteLine("Request failed.");
                }
            } catch ( Exception e ) {
                Console.WriteLine("Error was thrown: {0}", e.Message);
            }
        }
    }
}

You may well ask "Why bother?". Well actually I've been wanting to write some code in C# for a while but there just aren't enough utility libraries/classes about under sufficiently good licenses. I could do a load of P/Invoke magic to make use of C-libraries but that invoves a) recompiling on each platform and b) only using C-libraries available on multiple platforms. Using Perl gives C# immediate access to the vast wealth of modules on CPAN.

This makes wrapper classes around Perl modules pretty easy to implement :). Also the magic boxing and unboxing ability of C# means that I can use 'Scalar's pretty much just like 'Variants' in VB (which is more or less a good thing, I think).

cancel ×

comment

Slashdot Account

Need an Account?

Forgot your password?

Don't worry, we never post anything without your permission.

Submission Text Formatting Tips

We support a small subset of HTML, namely these tags:

  • b
  • i
  • p
  • br
  • a
  • ol
  • ul
  • li
  • dl
  • dt
  • dd
  • em
  • strong
  • tt
  • blockquote
  • div
  • quote
  • ecode

"ecode" can be used for code snippets, for example:

<ecode>    while(1) { do_something(); } </ecode>
Create a Slashdot Account

Loading...