Inline::MonoCS - Use CSharp from Perl, via Mono


Hello World

  use Inline::MonoCS
    method        => "HelloWorld",
    compiler_args => "",
    code          => <<"CODE";
  public class HelloWorld
      public static void Main( string[] args)
          System.Console.WriteLine( "Hello, " + args[0] + "!" );

  warn HelloWorld("Frank"); # "Hello, Frank!"

Talk to Microsoft SQL Server from Linux

  use Inline::MonoCS
    method        => "ProductCount",
    compiler_args => "-r:System.Data.dll",
    code          => <<'CODE';
  using System;
  using System.Data;
  using System.Data.SqlClient;

  public class ProductCount
      public static void Main(string[] args)
         string connectionString =
            "Server=;" +
            "Database=northwind;" +
            "User ID=sa;" +
         IDbConnection dbcon;
         using (dbcon = new SqlConnection(connectionString)) {
             using (IDbCommand dbcmd = dbcon.CreateCommand()) {
                 string sql =
                     "SELECT COUNT(*) AS ProductCount " +
                     "FROM Products";
                 dbcmd.CommandText = sql;
                 using (IDataReader reader = dbcmd.ExecuteReader()) {
                     while(reader.Read()) {
                         int ProdCount = Convert.ToInt32( reader["ProdCount"] );
                         Console.WriteLine( ProdCount );

  my $count = ProductCount();
  warn "We have $count products";


This module provides a simple bridge to execute code written in C# from Perl.

It works by compiling your code, then placing the executable under /tmp/ and exporting a subroutine into the calling package. When you call that exported subroutine, the compiled exe is executed and given your arguments on the command-line.

Whatever your program outputs to STDOUT is considered the return value.


Written by John Drago <>


This software is Free software and may be used and redistributed under the same terms as perl itself.