2009Q3 Grant Proposal: Creating a ctypes-Like Interface for Perl to External Subroutines


Shlomi Fish


[hidden email]

Amount Requested:



Develop a powerful Foreign Function Interface for Perl 5, similar to Python's ctypes.

Benefits to the Perl Community

ctypes provides a very handy way to call hard-coded subroutines in shared libraries, but only for Python. Having a similar module for Perl will benefit Perl 5 programmers who will be able to call subroutines in C and similar compiled languages from their Perl programs easily and dynamically.


A CPAN module which will give Perl 5 programmers similar functionality to Python's ctypes. ctypes is licensed under the MIT/X11 Licence ( http://en.wikipedia.org/wiki/MIT_License ) and we hope to preserve that licence for all the code derived from ctypes, and for the original code. Licensing the code under the MIT/X11 Licence will give its users and developers the most flexibility, and will make the end-result compatible with Parrot, Apache and other projects.

Project Details

ctypes ( http://python.net/crew/theller/ctypes/ ) is an advanced Foreign Function Interface (FFI) for Python. ctypes allows programmers to call native functions (in C, C++, etc.) from high-level code at run-time, without needing compilation, and without the need to be familiar with the high-level virtual machine's internals. This is as opposed to Perl's XS or Inline::C.

This proposed project aims to deliver a ctypes-like API for Perl which will provide similar capabilities.

chromatic mentioned a ctypes-like API in his "Five Features Perl 5 Needs Now" article:



  • Studying the ctypes API (1 week or less)
  • Studying the ctypes internals (2 weeks)
  • Designing an equivalent Perl 5 API (1 week)
  • Porting ctypes to Perl/XS (4 weeks or more)

Project Schedule

The project will take 2-3 months. I can begin working on it immediately.


I am an active user, developer, and advocate of Perl and other open-source technologies. I maintain many modules on CPAN (http://search.cpan.org/~shlomif/), and have contributed to other Perl projects. I am also proficient in C, C++, Assembly and other languages and have been actively involved in C and C++ projects.

I have successfully completed the Perl Foundation's "XML-RSS Cleanup" grant:

http://www.perlfoundation.org/news/post/May%202, %202007%20-%20XML::RSS%20Cleanup%20Grant%20Completed%20-%20Final%20Report

( short URL: http://is.gd/1nYZM )

I maintain an active homepage at http://www.shlomifish.org/ which contains more information about me.


This project will lower the bar for extending Perl to low level languages, while improving performance of modules that take advantage of it. This would be an awesome improvement to Perl 5.

Please fund this project!

XS seems terrifying to the average Perl programmer, and even highly skilled programmers try to avoid its hassle.

Having ctypes-like system for Perl would allow any Perl programmer to interact with shared libraries without the issues of XS.

I would have to say 2-3 months is little time to an average programmer but Shlomi Fish is indeed a very capable and prolific programmer, who contributes quite a lot to CPAN, Perl and Open Source communities, who could definitely get it done, and get it done right.

There is nothing I would like to see more than being able to use ANY shared library without fearing XS or (even) SWIG.

How exactly does what your proposing differ from the existing P5NCI module?

What is the difference between this and http://search.cpan.org/dist/FFI/ ??

Why not start from Win32::API? While it is IA32-only, a lot of the code should be easy to port to Linux/Mac, and it would be easier/quicker than starting from scratch (or from ctypes).

Yes, yes, a thousand times, yes.

A huge stumbling block is the difficulty in using third party libraries in Perl, where it's easy to do in Python. There are libraries out there to assist with this, but they only work well on a few platforms, or still require a significant amount of boilerplate to get started. Something like this would make bindings far easier to deal with in Perl, and enable Perl to flourish in new platforms like Maemo or Haiku.

Leave a comment

About this Entry

This page contains a single entry by Alberto Simões published on August 10, 2009 12:20 PM.

Find recent content on the main index or look in the archives to find all content.