Perl, C and Dr. Dennis Ritchie

As many of you have read in the news Dennis MacAlistair Ritchie has passed away at age 70. Given numerous awards for creating the C programming language and developing UNIX Dr. Ritchie is not just a giant among men, he is a titan in the computer industry. Here is a video of Ritchie and Thompson receiving an award from then President Bill Clinton.

dmr (Ritchie) along with Dr. Brian Kernighan wrote "The C Programming Language" which is considered the authoritative reference on C and is commonly referred to as K&R or the K&R book. Personally I have read the second edition covering ANSI C multiple times. It is an easy read and not very long weighing in at 272 pages. In comparison it is a bit longer than "Modern Perl"'s 176 pages but shorter than "Higher Order Perl"'s 582 page read. K&R is a moniker used to refer to a bracing/indenting style, a version of C, a book, and two computer science engineers (it also happens to be my first two initials as well). The impact of dmr's work is felt.

Some examples of projects that use C include the Linux Kernel, Android, dalvik, the Oracle JVM, Parrot, Perl 5, Apache's HTTP server, git, etc... I think people tend to forget how important C still is to everyday computing. Some of the absurd reasoning I hear is that C is too old or is only for systems programming not for anything cutting edge thus people should not use it.

The Quake, Quake 2 and Quake 3 video games were written in C and achieved widespread success commercially with gamers. Then these games were open sourced and released under the GPL for others to learn from and build upon. A Perl hacker took this to heart and embedded the Perl interpretor into Quake 2.

The Perl ecosystem has grown richer due to C and vice versa, take the explosive growth of the Internet for example. Apache's httpd was at the forefront of the pack in the early days and is still a market leader today. An open source web server which quickly expanded and adapted to meet the expanding needs of the web. One of these needs was CGI programs and Perl fit right in. It kept gaining popularity and projects like mod_perl (written in C and the test suite is in Perl) were launched to help meet the requirements of more speed, scalability and power.

Perl growing in market share from the web is all fine and dandy but I have a bunch of C code I would still like to use. Is this possible? In a word, yes. Programmers can use C directly in Perl via the CPAN module Inline::C. Following the famous example from the K&R book here is a hello world program from the Inline::C-Cookbook


use Inline C => <<'END_C';

void greet() {
printf("Hello, world\n");
}
END_C

greet;

Want to call C libraries from Perl? You can wrap the library with swig and generate a Perl interface or do it with perlxs.

Do you want to create C code in Perl at runtime? Language::XS can do that for you.

Want to call Perl and its functions from inside a C program? Check out 'perldoc perlembed' for numerous examples.

Want to use the K&R or other brace style with Perl? Sure no problem. perldoc perlsyn explains it best:

Perl is a free-form language, you can format and indent it however you like. Whitespace mostly serves to separate tokens, unlike languages like Python where it is an important part of the syntax.

Many of Perl's syntactic elements are optional. Rather than requiring you to put parentheses around every function call and declare every variable, you can often leave such explicit elements off and Perl will figure out what you meant. This is known as Do What I Mean, abbreviated DWIM. It allows programmers to be lazy and to code in a style with which they are comfortable.

Perl borrows syntax and concepts from many languages: awk, sed, C, Bourne Shell, Smalltalk, Lisp and even English. Other languages have borrowed syntax from Perl, particularly its regular expression extensions. So if you have programmed in another language you will see familiar pieces in Perl.

Speaking of things getting borrowed from Perl there is a Perl Compatible Regular Expressions (PCRE) library for C. This library in turn is used by modern versions of Apache httpd.

The Test Anything Protocol (TAP) has been ported to libtap for use with testing in C.

The list goes on but there is obvious cross pollination between the Perl and C communities and this is a very good thing. I think it shows a level of maturity and openness that seem to get lost on programmers who lump themselves into "camps" and believe they use the one true language. Everybody already knows the one true language of ultimate power and endless fun is Brainfuck. ^_^

I mean look at this hello world program:


++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

I only have to hit a few keys to do anything and the syntax is not that complicated.


Moving along Jason Victor's blog has an interesting comment about C as a language:

In my opinion, the reason C has maintained its popularity for all these years is that it is relatively paradigm-free. Most languages, for better or worse, come equipped with some inherently recommended programming paradigm – object-oriented, functional, whatever.

Sounds a good deal like the classic Perl motto TMTOWTDI.

In summation, I just want people to remember that the Perl community is not stuck on an island in the middle of the ocean alone. There are other vibrant communities out there that you might contribute to or get something from. Do not limit yourself to a single camp or idea.

With that in mind please take a moment and watch this short clip where Ritchie and Thompson talk about unix and their goals. TL:DW "What we wanted to preserve was not just a good programming environment in which to do programming but a system around which a community could form, a fellowship."

Sounds familiar doesn't it. I think the Perl community and the efforts with CPAN are in this very spirit. CPAN helps do programming but adding in the ticket system, annotating POD, cpan ratings, irc channels and mailing lists and you have a community. Users and authors can communicate ideas and the community as a whole receives the benefits.

dmr your amazing presence will be missed. Here are some "Hello World" programs in honor of dmr.

I decided to add a few in Perl.

Perl:


#!/usr/bin/perl

print "Hello World\n";

Modern Perl (C-ish):


#!/usr/bin/perl

use v5.12;

sub main {
say 'Hello World';
return;
}

main() unless caller;

1;

The one liner:


perl -E 'say "Hello World"'

Comments, suggestions, one liners post em if you got 'em.

2 Comments

Ode to Dennis Richie

#include 

main ()
{
printf( "Goodbye, Dennis\n" );
}

Great post! Perl and C are the two greats!

Leave a comment

About Kimmel

user-pic I like writing Perl code and since most of it is open source I might as well talk about it too. @KirkKimmel on twitter