The T-Files


Sat, 06 Mar 2004

The thousand and one reasons to love Perl: [1] Inline::C

Consider this: You have to add support for some electronic payment system to your shopping website. You have to access the payment gateway using a vendor-supplied C library. The documentation suggests you write a CGI script in C, or use the Java Native Interface. Well, Perl to the rescue!

Perl is written in C (Perl6 will be, too). There is an mechanism to create Perl extensions in C, providing access to native libraries. This is frequently used to make Perl interfaces for things like XML parsers, database drivers, OpenSSL, or efficient mathematical algorithms. Using that mechanism is not for the faint of heart however, as it has a steep learning curve. But in Perl, there is always More Than One Way To Do It (tm), and in this case there is Inline::C.

Inline::C is a module that allows you to write Perl subroutines in C. You can just place them in the middle of your Perl files (hence the name, Inline). You can also access external libraries. All of this is surprisingly hassle-free with compilation and linking going on behind the scenes.

use Inline C => Config =>
    ENABLE => 'AUTOWRAP',
    LIBS => 
        '-L/usr/local/edy/lib -L/usr/local/ssl/lib -lssl -lcrypto -ledymallapi',
    INC => '-I/usr/local/edy/include';

use Inline C => <<'EDY';

#include<edymallapi.h>
#include<edyerrcode.h>

# just declare the function headers, wrappers around the
# C functions in the edymallapi library will be automagically
# created

int edygeterrorcode();
char *edygeterrormessage( int );
EDY

# and now I can call the C function from Perl
# just like that

print edygeterrormessage( edygeterrorcode());

Have a look at the Inline::C cookbook.

Roger Stern: The Death and Life of Superman

The foreign language section of the Shibuya public library has some rather annoying gaps. There is for example not a single volume of Philip K. Dick, Douglas Adams is limited to Mostly Harmless, no sequel to Hyperion and no Mrs. Dalloway. They make up for this by having what seems to be a complete collection of Stephen King, Danielle Steele, and John Grisham, supplemented by sports biographies and adaptations of Star Trek, Star Wars, comic book, and TV shows. I can only hope that those tumbled out of an expatriate bookshelf in the form of a donation, rather than being the result of tax yen spent (in this case, building superfluous bridges and roads instead does seem a good choice).

Stern's novel is the write-up about the events leading to Superman's death and rebirth in 1992/1993. It is based upon the comic books published during this period, with additional material drawn from other volumes of this long-running series. So if you want to learn about Superman's encounter with Doomsday, his dealings with Lois Lane, his foster parents in Kansas, the people and police of Metropolis, Lex Luthor, Supergirl, the Justice League, the media, Superboy, several clones, impostors, and aliens, but you do not want to read the comics itself, this book is for you. I said if.