Talking about the relationship between C / C ++ and Perl

I will talk about the relationship between C language / C ++ and Perl.

Perl is written in C

Perl is written in C. The Perl programming language is written in C and is expected to be compiled by a C compiler such as gcc.

In this respect, Perl is dependent on the C language.

Also, Python, PHP, and Ruby created after Perl are also written in C language. Writing a dynamic programming language in C is widespread, but the Perl example was early on.

C and programming languages ​​

C is a good programming language for creating practical programming languages. C language can be compiled in many OS environments, and after compilation, it becomes a machine language, and memory operations such as memory allocation and access by memory address position can be freely performed.

Perl is a memory-safe programming language, but when writing a memory-safe programming language, write memory-safe programming logic in C language, which is not memory-safe.

C language seems to have a policy of not allocating memory in the library as a general rule, except in exceptional cases. There are no functions that require memory allocation logic, such as string substitution, dynamic arrays, and associative arrays.

This is not the inferiority of C language, but it seems to be the philosophy of C language that memory allocation is basically the responsibility of the user.

If you need string substitutions, dynamic arrays, associative arrays, etc., you have to implement them yourself.

Call C from Perl

Perl has a mechanism called XS for calling C language functions. XS is written in C, compiled into a shared library (.so, .dll) and can be called from Perl.

This means that a library written in C can be bound by calling the header and source files from the XS, if any.

Also, if it is a shared library, you can call it.

XS, FFI, Inline::C, SPVM, and XS is one of them.

Call C ++ from Perl

Perl has a mechanism called XS for calling functions in C language, but the machine language in C ++ is practically the same as the machine language generated from C language. The only difference between C ++ and C is the function call name. Therefore, you can call a C ++ function by changing the call name of the C ++ function to the call name of the C language.

This can be done using the "extern" C "" syntax.

extern "C" {
  // C ++ functions
}

Libraries written in C ++ can be bound by calling the header and source files from the XS, if any.

Also, if it is a shared library, you can call it. Being a shared library means that it is already in the C language function call format.

The mechanism for calling C ++ from Perl is XS, FFI, Inline::C, as in C language. For example, SPVM.

Associated Information