Exploring Strange Languages - Programming on Linux

Novell Cool Solutions Feature
By Kevin Burnett

Posted: 21 Jul 2005

Kevin Burnett
Software Engineer
Novell, Inc.
[email protected]

Introduction
Python on Linux
Why Do Linux Users Like Python So Much?
Downside To Python?
Where To Get Python
Perl on Linux
Perl Features
Perl and the Internet
Basic Syntax
Where To Get Perl
PHP on Linux
PHP History
Database Connectivity
PHP and Linux
PHP Stats
Zend.com
Basic Syntax
Where to get PHP
Ruby on Linux
Features of Ruby
Ruby: A Bit of History
Basic Syntax
Where to Get Ruby
Summary

Introduction

While most developers use popular languages such as C, C++ and Java to create solutions on Linux, others are using somewhat obscure or strange languages to create Linux solutions. This article will look at providing Linux solutions using such programming languages as Python, Perl, PHP, and Ruby.

Python on Linux

Perl is the most popular scripting language, by a long shot. However, on Linux, Python is gaining a loyal following. It can be looked at as the other scripting language that starts with "P". It was named for Monty Python, the comedy troupe, and references to their skits and films (most commonly to their hilarious "Spam") appear frequently in Python programs. Python has a number of advantages over other languages like Java and Perl, some of which we will look at here.

Most Linux distributions have Python installed. Now this doesn't guarantee that you will get the latest and greatest build with your distribution. If you want a newer version, you can build from the source; sometimes RPMs are also available. You can go to http://www.python.org/download/download_linux.html. This site has all things Python when it comes to the processor.

Now let's look at a simple Python program.

#!/usr/bin/env python
print "Hello World"
This is the "famous" programmer's Hello World test. This will produce the result:
Hello World

For the sake of comparison with the much more popular Perl,

#!/usr/bin/perl
print "Hello World\n";

One thing to look at with the Python code is this line:

#!/usr/bin/env python

A big plus for Python is portability. Using the #! line rather than the actual location of python on your system allows you to move this code, without modification, to any system that has /usr/bin/env (the equivalent #! line for Perl points directly at the Perl binary). The end result is the flexibility of running Python code on more platforms than currently supported by Java. To up that ante, you can even run Python code within a Java Virtual Machine, using Jython (http://www.jython.org/).

Another plus for Python is that since it is an open-source language, it is not subject to the same sort of OS-specific tweaking that Java is known for.

Since Python is a scripting language, you don't have to define constants or variables before you use them. This is very much like the classic Basic scripting language. This can make development a lot easier, since you can try out alternative solutions to a problem quite easily.

Another nice thing about Python, compared to Perl, is that it doesn't require a semicolon (;) at the end of a line. How many times, when coding in C, do you forget to terminate an expression with a semicolon? Well, Python gives you the luxury of not needing this.

Also you don't have to add newlines in your print statements, as in "Hello World\n", as you do in Perl. This is because the print statement in Python is smart enough to assume that you usually want a newline after your prints. If you don't want a newline, just add a comma at the end. The comma operator inserts a space:

print "hello", "world"

Will produce:

Hello World

If you want to concatenate "hello" and "world" without a space in between, do the following:

print "Hello" + "World"

Which produces:

HelloWorld

We see from these few simple examples, that Python is quite easy to use. Granted, it is quite flexible, and to utilize that flexibility you will need to study the syntax on one of the great websites available or pick up a Python book at your local bookstore.

Why Do Linux Users Like Python So Much?

  • Linux users and programmers like Python for several varying reasons. Granted, the Python followers numbers still pale in comparison to the Perl users, but those who have tried and continue to use Python do it for the following and other reasons:

  • Clarity. Python's coding structure makes it almost impossible to write messed-up or spaghetti code. White space is used to delimit blocks, ensuring obvious block structure. The syntax is clean, with a consistent calling structure for modules and functions.
  • Preference. For some Python is their preferred language because it is an elegant, clean programming language. Others prefer it because of its organized look and logical, progressive design elements.
  • Object support. Python will let you implement object-oriented designs without having to know the heavy-duty syntax required for C++ or Java.
  • Development speed. Python is a rapid prototyping language. It provides strong support for GUI toolkits like GTK. Additionally, Python lets you craft full-featured solutions quickly. If you are not happy with the performance of the resulting Python program, it is quite easy to plug in C replacements for the performance-critical components on a piece-by-piece basis.
  • Interoperability. Python is very portable code, with interpreters available for all common, and many uncommon, operating systems. So you can develop code on Linux and deploy the same code throughout your system.
  • Libraryability. OK, so Libraryability is not a real work, but Python is a perfect language for creating small, well programmed components, called modules. Since modules are very easy to design and use, this encourages formal and informal code libraries. As a result, these libraries can be used, re-used and posted on the Internet for other to use.
  • Reliability. High-quality coding standards are encouraged by the Python community. An example is the common practice of writing modules that contain their own test routines.
  • No Compiling. Python is a byte-compiled language, but it is also an interpreted language. Translated, this means that the first time the Python interpreter executes a program or a module, it is translated into byte code. This byte code is written to disk as .pyc. Then, when the program or module is executed again, the interpreter checks to see if there is a .pyc file with a more recent timestamp than the .py file. If one exists, it skips the byte-compilation step and reads the compiled file from disk. This can reduce execution time.
  • Potpourri of class libraries. Class libraries are available for most popular programming tasks, such as math libraries, support for several different GUI toolkits, support for CGI programming and HTML and XML generation and parsing, just to name a few. See http://python.org for all that is currently available.

Downside To Python?

If there is a downside or bad side to python, it would have to been for the use of device drivers, kernels, and other low level devices. Python is too high a level language to allow you to interact with the hardware specifics of a computer or other device. Better to leave that development to C/C++ and Assembly programming.

Where To Get Python

You can get all things Python, including excellent documentations from http://python.org, although there are many other websites that offer Python libraries, etc. It would be well worth an Internet search to see everything that is available out there.

Perl on Linux

I had to talk about Python first, because I always like to go for the underdog. So, now let's switch our focus to a much more popular language in the Linux world, Perl.

Perl was created by Larry Wall. He continues to develop and maintain the language with the help of the Perl community. Perl is available on virtually every computer platform.

Perl is short for "Practical Extraction and Report Language", however, sometimes you may hear it referred to as the "Pathologically Eclectic Rubbish Lister." It started out as a scripting language to supplement rn, the ubiquitous USENET reader, which Wall also wrote.

Perl is an interpreted language that is optimized for string manipulation, I/O, and system tasks. It is very popular with system administrators.

Perl continues to attract attention with the ever popular World Wide Web as a quick and effective way to mock up applications that provide much of the web's interactivity. Also, there are few of the traditional limitations that interpreted languages impose.

Perl Features

  • Perl takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among others.
  • Perls database integration interface (DBI) supports third-party databases including Oracle, Sybase, Postgres, MySQL and others.
  • Perl works with HTML, XML, and other mark-up languages.
  • Perl supports Unicode.
  • Perl is Y2K compliant.
  • Perl supports both procedural and object-oriented programming.
  • Perl interfaces with external C/C++ libraries through XS or SWIG.
  • Perl is extensible. There are over 500 third party modules available from the Comprehensive Perl Archive Network (CPAN).
  • The Perl interpreter can be embedded into other systems.
  • Perl supports varied platforms including Unix, Macintosh, Windows, WMS and many more.

Perl and the Internet

Perl is the most popular web programming language due to its text manipulation capabilities and rapid development cycle. It is widely know as the duct-tape of the Internet. HTML forms can be handled simply with Perl's CGI.pm module. Perl is fully capable of handling encrypted Web data, including e-commerce transactions. It can also be embedded into Web servers to speed up processing by as much as 2000%. Also, Perl is very easily integrated with databases.

Basic Syntax

The following is information that applies to all versions of Perl. Perl is what is called free form, in other words, whitespace doesn't matter. Most people like to use liberal indentation and top down design, but you could write all on one line, if you're a masochist.

  • All Perl statements end in a ; (semicolon), like C.
  • Comments begin with # (pound sign) everything after the #, and up to the end of the line is ignored.
  • The # needn't be at the beginning of the line.
  • There is no way to comment out blocks of code other than putting a # at the beginning of each line.
  • It's a convention taken from the shell utilities.

Now let's look at a simple Pearl program.

#!/usr/local/bin/perl
print "Hello, world\n";

This is the "famous" programmer's Hello World test. This will produce the result:

Hello World

One thing to note, which was mentioned above, is that you need to place a "\n" when you want a newline.

The first line of the program specifies the location of the Perl interpreter. On Linux servers, the Perl program is usually installed in the /usr/local/bin directory. This special syntax is the proper way of indicating to a Linux operating system that it should use this interpreter to read the next lines of code.

The second line of the program is a simple Perl print statement that prints out the now-famous phrase. When you run this program from the command-line, the words "Hello, world" are printed to your screen (followed by a carriage-return).

Just save these two lines of code to a file named hello. Then, to run the program on a Linux system, you'll need to make the program executable. You can do this with the chmod command:

chmod +x hello

Once the file is made executable, you can run it by entering the name of the file, like this:

hello

Note: If your current directory isn't in your path, you'll need to change your PATH variable, or run the program like this:

./hello

Where to Get Perl

For more information about Perl, there are a variety of site on the Internet, but the main one would be http://www.perl.org.

PHP on Linux

PHP, also known as PHP: Hypertext Preprocessor, is an open-source server-side scripting language. It is freely downloadable from php.net and zend.com. PHP can be used for creating dynamic Web pages. (A dynamic Web page is a page that interacts with the user, so that each user visiting the page sees customized information.) Dynamic Web applications are common in commercial sites, where the content typically comes from a database.

One of PHP's strong features is that it offers a simple solution for easy-to-program dynamic Web pages. The interface allows programmers to place PHP commands right in the HTML page. PHP's syntax is similar to that of C and Perl, making it easy to learn for anyone with basic programming skills. Its elegant design makes PHP significantly easier to maintain and update than comparable scripts in other languages.

PHP History

PHP is an open source product. It has a wide distribution to a large community of users. PHP is very well supported, enjoying the support of a large group of open-source developers. The community gives excellent technical support to users, and bugs are found and repaired quickly. The code is continuously updated with improvements and language extensions to expand PHP's capabilities.

On the historical side, PHP has been in development since 1994. The current release, PHP 4.4, was released in 2005, has gained rapid popularity, and is now used in Web-related applications by some of the most prominent organizations such as NASA.

Database Connectivity

PHP offers excellent connectivity to most of the common databases. Included in the compatibility is Oracle, Sybase, MySQL, and many others. PHP offers integration with various external libraries, which allow the developer to do anything from generating PDF documents to parsing XML. Another plus for PHP is that it is open-sourced (see http://www.php.net). In addition, it supports many hardware and software platforms.

PHP and Linux

PHP is made for Linux. It is also made for the Apache server software, but runs equally well on any other UNIX or Windows platforms. Granted, the Apache Web Server works great with PHP, but it will also work with Netscape or Microsoft Web server software on the Windows platform. PHP supports lots of common Web technologies and specifications such as HTTP sessions, Java connectivity, regular expressions, LDAP, SNMP, IMAP, and COM (under windows) protocols. It also supports WDDX complex data exchange between virtually all Web programming languages.

PHP is no dog when it comes to execution. It does offer quite good performance. The combination PHP-Apache-Linux features instantaneous access times, with even greater performance boosts achieved by caching data structures on the server.

PHP Stats

PHP has really taken off for dynamic Web pages. According to a Netcraft survey of what technology is actually in use on the Web, PHP can now be found on more that 6 million domains, and is growing at a rate of up to 15% each month. PHP is available on over 36% of Apache Web servers – the most common server on the Web. The latest version of PHP, PHP 4.4, was downloaded 265,000 separate times in the first two months it was available. With the open-source trend gaining popularity, PHP is expected to continue to pick up momentum.

Zend.com

Zend.com is a one-stop resource for the PHP community seeking PHP-related information and support services. The site provides commercial backing for PHP through a range of products available through the Zend website. For more information, see http://www.zend.com.

Basic Syntax

When coding with PHP, keep these few things in mind:

  • PHP is server-side. Your browser doesn't realize the pages it is viewing are initially written with PHP. All it receives is an HTML page - as complex or as simple as you want.

  • PHP is HTML-embedded. A PHP page can be simply an HTML page with a little PHP sprinkled here and there.

  • The PHP bits are parsed ("translated") by the server. The HTML code on the page is sent directly to the browser.

  • PHP has similarities with other programming languages. C and Perl are two of them. It may be helpful to learn some Perl to help you understand what PHP could do and how it works.

So here is the famous "hello world" in PHP.

<?php
echo 'Hello World';
?>

With the output looking like this:

Hello World

What is happening is as follows:

<?php starts PHP code
echo 'Hello World'; writes Hello World
?> stops PHP code.

Remember that the PHP code is typically embedded in an HTML page and that it is processed by the Web server and not your Browser of choice.

Where to get PHP

There are several good websites that either devote their entire site to the virtues of PHP or at least a portion of the site. The defacto standard would be http://www.php.net/, where you can start out by following their beginners tutorial or use the site as a full blown reference site.

Ruby on Linux

Ruby is an interpreted language that allows for quick and easy object-oriented programming. It has many features, some similar to Perl, that all the processing of text files and also allow you to do system management tasks. It is quite easy to learn and use, is straight-forward, extensible, and portable.

Ruby is also Open-Source, meaning that it is does not require a licensing fee and also allows you the freedom of choice to use, copy, modify, and distribute it.

Features of Ruby

The following is a partial list of features that Ruby provides. For a full list and all the information you would ever want about Ruby, check out their website at: http://www.ruby-lang.org/.

  • Ruby has simple syntax, partially inspired by Eiffel and Ada.

  • Ruby has exception handling features, like Java or Python, to make it easy to handle errors.

  • Ruby's operators are syntax sugar for the methods. You can redefine them easily.

  • Ruby is a complete, full, pure object oriented language: OOL. This means all data in Ruby is an object, in the sense of Smalltalk: no exceptions. Example: In Ruby, the number 1 is an instance of class Fixnum.

  • Ruby's OO is carefully designed to be both complete and open for improvements. Example: Ruby has the ability to add methods to a class, or even to an instance during runtime. So, if needed, an instance of one class *can* behave differently from other instances of the same class.

  • Ruby features single inheritance only, *on purpose*. But Ruby knows the concept of modules (called Categories in Objective-C). Modules are collections of methods. Every class can import a module and so gets all its methods for free. Some of us think that this is a much clearer way than multiple inheritance, which is complex, and not used very often compared with single inheritance (don't count C++ here, as it has often no other choice due to strong type checking!).

  • Ruby features true closures. Not just unnamed function, but with present variable bindings.

  • Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or converted into closures.

  • Ruby features a true mark-and-sweep garbage collector. It works with all Ruby objects. You don't have to care about maintaining reference counts in extension libraries. This is better for your health. ;-)

  • Writing C extensions in Ruby is easier than in Perl or Python, due partly to the garbage collector, and partly to the fine extension API. An SWIG interface is also available.

  • Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances of class Fixnum) and large integers (Bignum), but you need not worry over which one is used currently. If a value is small enough, an integer is a Fixnum, otherwise it is a Bignum. Conversion occurs automatically.

  • Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples: simple 'var' = local variable, '@var' = instance variable, '$var' = global variable. So it is also not necessary to use a tiresome 'self.' prepended to every instance member.

  • Ruby can load extension libraries dynamically if an OS allows.

  • Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading, regardless of if the OS supports it or not, even on MS-DOS.

  • Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/Me/NT/2000/XP, MacOS, BeOS, OS/2, etc.

Ruby: A Bit of History

Ruby was created on February 24 1993 by Yukihiro Matsumoto. The following is from his Web Blog:

"I was talking with my colleague about the possibility of an object-oriented scripting language. I knew Perl (Perl4, not Perl5), but I didn't like it really, because it had smell of toy language (it still has). The object-oriented scripting language seemed very promising.

I knew Python then. But I didn't like it, because I didn't think it was a true object-oriented language–OO features appeared to be add-on to the language. As a language manic and OO fan for 15 years, I really wanted a genuine object-oriented, easy-to-use scripting language. I looked for, but couldn't find one.

So, I decided to make it. It took several months to make the interpreter run. I put it the features I love to have in my language, such as iterators, exception handling, garbage collection.

Then, I reorganized the features of Perl into a class library, and implemented them. I posted Ruby 0.95 to the Japanese domestic newsgroups in Dec. 1995."

Since then, highly active mailing lists have been established and web pages formed to declare the Ruby message. Once again, for all things Ruby, see http://www.ruby-lang.org.

Basic Syntax

Ruby is developed under Linux, and is written in fairly straightforward C. It runs under UNIX, DOS, Windows 95/98/NT/2000, Mac OS X, BeOS, Amiga, Acorn Risc OS, and OS/2.

Under Unix, Ruby uses the autoconf system to configure the build environment. On Linux, you don't need the autoconf command on your box to build Ruby from a distribution; just use the commands:

% ./configure [configure options]
% make
% make test
% make install

You may need superuser privileges to install Ruby if you don't override the default installation location (/usr/local). You can get a full list of configure options using:

% ./configure --help

The following is the code needed to do the famous "Hello World."

# Hello World in Ruby
STDOUT << "Hello World"

With the display showing the following output:

Hello World

Where to Get Ruby

The best place to get Ruby information would be the official Ruby website: http://www.ruby-lang.org/. However, there are several other websites that have information about Ruby. After visiting the official site, look around the Internet to get your specific questions answered.

Summary

Linux is a rich operating system providing almost anything you might need. The kernel is available for customization, satisfying the diehard programmer, while there are a variety of programming languages for the more traditional and recreational programmer. What I have tried to accomplish with this article is to perk your interest in some of the not so popular programming options available on the Linux platform. Granted, there are more, and many of these are very specialized, but what I have presented here should appeal to the general programmer.