Channels ▼
RSS

Open Source

Pike Programming Language


This article is about a language that has been relatively undiscovered but is full of interesting features: Pike. It is open source, of course. The syntax of expressions and control statements in the language is very similar to C, so C programmers will find it easy to start coding in Pike or port their code with minor changes. The language is also object-oriented, with automatic memory management, high-level data types, powerful string- and text-processing features, excellent support for network and I/O operations, dynamic loading of pre-compiled shared libraries written in C, and built-in support for event-driven programming via a native event loop.

Pike is a well-tested language that is used in commercial applications such as the Roxen Web server and Turbo proxy servers. Turbo proxy servers are developed by Opera Software to accelerate the browsing speed in Opera Web browsers. The servers are placed between the client and the Web server and their function is to compress the incoming data before it is sent to the client. This step speeds data transfer and reduces bandwidth usage. The code that runs on Opera's Turbo servers to perform efficient data compression is written in Pike.

In a nutshell, Pike is a language that is highly optimized for string and text processing (like Perl), portable (like Java but more flexible), suitable for functional programming and Lambda calculus (like Haskell), appropriate for concurrent applications, multi-purpose and flexible (like Python), but with strict type checking and casting (like Ada).

What is Pike?

Pike is available on major operating systems, including UNIX, Windows, and Mac OS X. The compiler is actively under development. The language was started as a student project in Sweden in 1994, and was later influenced by other languages (C, LISP, and Java).

The original purpose of Pike was to write Multi-User Domain (MUD) games. Therefore, it was based on sets, maps, and dynamic arrays, and used typing and other modern language constructs (since it was much easier to handle rooms, inventory, and dialogs, using these data types in a MUD game). As mentioned earlier, the syntax of Pike is quite close to C. For instance, the canonical program for all languages when written in Pike is:

int main()
{
	write("Hello world!");
	return 0;
}

Some additional important characteristics of Pike include:

  • Pike's object-orientation is robust with multiple inheritance, encapsulation, operator and function overloading, iterators, etc.
  • Exception handling is efficiently implemented in Pike — exceptions can be thrown within a function and thrown exceptions can be caught and handled as in most modern programming languages.
  • Pike programs are compiled at runtime. Each part of the program is first compiled into bytecode (similar to what is done in Java) and then interpreted into machine-code at runtime. Pike also supports implementation of callback functions (function pointers) and pre-compiled versions of a class.
  • Memory management is done using garbage collection (GC), which I describe later. Developers are free to mark an object as a candidate to be collected by GC when it is no longer needed.
  • Different modules can be written in Pike, C, or C++ and can easily be integrated into a Pike program.
  • Lambda abstraction and function mapping, which are essential parts of functional programming, are supported in Pike. Using Lambda abstraction, it is possible to directly define a function when needed. This feature can be handy when it comes to callback functions, where the functions can be defined directly in place. As an example, in the following code, a function has been defined in place that will perform some clean-ups by informing the GC that there are instances of previously defined classes that can be marked as garbage before a file is closed:
    // do some clean-ups before closing a file...
    set_close_callback(
        lambda(class1 c1, class2 c2) { destruct(c1); destruct(c2);} );
    

    In addition, all operators in Pike are treated as functions, as with the following expressions, which give the same results:

    > '+(1,'*(2,3)) ;
    (1) Result: 7 
    > (2 * 3) + 1; 
    (2) Result: 7
  • I/O operations and networking can be done in a straightforward manner in Pike. A negative return value from main() will simply put the master program in asynchronous mode, which is in fact an event processing loop. More precisely, the negative return value acts as a shortcut to signal the master process to start a back end for asynchronous code. In asynchronous mode, all incoming external events can be handled by adding appropriate callback functions. Therefore, writing a server that responds to incoming connections simultaneously is possible in just a few lines of code. Additionally, thanks to the rich libraries that are shipped with Pike, advanced networking can be implemented easily and efficiently. The sample code shown below demonstrates a simple server in Pike. Returning a negative value by the main() function will put the program into the event loop, which is listening on PORT and handling any incoming connection by calling the appropriate callback function (accept_callback in this example).
    #define PORT 1905
    
    int main(int argc, array(string) argv)
    {
       werror("Starting minimal httpd...");
    
       if (!bind(PORT, accept_callback))
       {
          werror("Failed to open socket (already bound?)");
          return 1;
       }
    
       return - 1; /* keep going */
    }
    


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video