Linking languages

后端 未结 9 1367
野趣味
野趣味 2020-12-31 06:16

I asked a question earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C+

9条回答
  •  抹茶落季
    2020-12-31 07:07

    If you choose Perl there are plenty of resources for interfacing other languages.

    Inline::C
    Inline::CPP
    Inline::Java

    From Inline::C-Cookbook:

    use Inline C => <<'END_C';
    
      void greet() {
        printf("Hello, world\n");
      }
    END_C
    
    greet;
    

    With Perl 6 it gets even easier to import subroutine from native library code using NativeCall.

    use v6.c;
    
    sub c-print ( Str() $s ){
      use NativeCall;
    
      # restrict the function to inside of this subroutine because printf is
      # vararg based, and we only handle '%s' based inputs here
    
      # it should be possible to handle more but it requires generating
      # a Signature object based on the format string and then do a
      # nativecast with that Signature, and a pointer to printf
    
      sub printf ( str, str --> int32 ) is native('libc:6') {}
    
      printf '%s', $s
    }
    
    c-print 'Hello World';
    

    This is just a simple example, you can create a class that has a representation of a Pointer, and have some of the methods be C code from the library you are using. ( only works if the first argument of the C code is the pointer, otherwise you would have to wrap it )

    If you need the Perl 6 subroutine/method name to be different you can use the is symbol trait modifier.

    There are also Inline modules for Perl 6 as well.

提交回复
热议问题