ITWales.com

An introduction to scripting languages

Dafydd Rees
 

Scripting languages are often used for one-off programming jobs and for prototyping. Scripting is also used in some large generic applications as a flexible way to configure generic software components to fit specialist requirements. Today a bewildering variety of scripting languages offer a range of powerful features. This article will illustrate some practical applications of scripting and provide an introduction to some of the most widely used scripting languages.

Scripting languages are programming languages. Although it's difficult to arrive at a definitive definition of "scripting", a scripting language is likely to be untyped (or weakly typed), interpreted and run from plain-text files. Scripting languages often emphasise ease of development over runtime efficiency and ease of deployment. From this point of view it's tempting to think that scripting languages are inferior to ordinary "production" programming languages. Rather than considering them to be inferior, consider that they offer a different set of strengths and weaknesses to conventional programming languages. This is something that becomes apparent by examining the practical uses of scripting.

Scripting languages allow programs to be written more quickly because they're more terse, assume a dynamic, interpreted environment (where parts of the program can change at runtime) and because they often contain powerful built-in operators and data types such as hash tables and expandable lists.

Practical uses of scripting

Systems administration

If you're reading this article on a Windows, Linux or Unix-based system the chances are that one or more scripts played an important part setting your system up for use when the power was turned on. Professional Unix systems administrators and webmasters often write specialised scripts to control internet and intranet servers - automatically performing maintenance tasks such as performing backups, checking for service failures and performing "one-off" data conversion chores. If you're thinking of becoming a systems administrator or a database administrator, you should seriously consider learning at least one of the scripting languages that are used on the platform with which you'll be working.

Automating software development

Software developers often perform the same routine tasks, for example building programs from a set of resource files, launching tests, writing header files, checking a set of changed files back into a source-code control system and building a complete image of a product ready for release. In a development team there are thousands of minute variations in the ways that manual development tasks can be configured. In product development, an inconsistency or even a fault could be introduced by a tiny difference between your development environment and that of another developer. Such problems could occur simply because you didn't realise that the order in which two particular tasks are performed is important. If you and your team perform the same tedious tasks frequently those tasks are candidates for what software consultants Andy Hunt and Dave Thomas call "ubiquitous automation". [1] Learn to use a scripting language to automate those tedious tasks. Apart from reducing the risk of introducing inconsistencies, it's easier to concentrate on development when you're not trying to remember long sequences of commands, or a series of mouse clicks. Scripting can often be incorporated into a build system. The ant build system, for example, allows developers to write customised tasks in a variety of scripting languages. [2] We can afford this level of automation because scripting languages are designed to automate frequently performed tasks rather than to directly support the construction of large, robust, consumer applications. This use of scripting is similar to the construction of moulds, templates and jigs by craftsmen.

"Common-off-the-shelf" package customisation

Although scripting systems have their roots in the old text-based systems of the sixties and seventies, the later half of the nineties saw many modern, graphical systems being retrofitted with powerful script and component-based facilities that allow a considerable amount of automation. Both Apple and Microsoft have continued to integrate new scripting infrastructure into their systems in recent years. On the windows platform, old batch-file scripting has been augmented by a new system called Windows Scripting Host (WSH). Windows scripting host is an environment in which various scripting languages can be installed. [3] (By default, WSH comes installed with VBScript and Jscript, Microsoft's Javascript-like language.) Once installed into the scripting host environment scripts written in these languages can drive various parts of the system and programs that implement "ActiveX automation". I have worked on one project in a Microsoft-based environment where a simple WSH script was used to automate the process of converting documents from an internal format into Microsoft Word format.

Large, generic software packages often cater for several different types of users. Both a technical publisher and a legal practice for example may buy the same word processor. Both the technical author and the legal practice are likely to have specialist tasks that could be automated in the word processor. Rather than try to keep up with an ever increasing list of specialist features, software houses often introduce a programmable "macro" or "scripting" facility into their product so that third party vendors and power-users can customise these general packages for specialist use. In Microsoft Office, for example, there is a built-in scripting environment called Visual Basic for Applications (or VBA). In a similar manner, the StarOffic package from Sun Microsystems can be customised by scripting in the OfficeBasic and Javascript languages.



A view of the macro recorder within Microsoft Word. Here you can see a script that was produced by "recording" a simple series of actions within Word.


Perl

Perl (Practical Extraction and Report Language) is a language that has its origins firmly in the culture surrounding the Unix operating system. Part of the Unix philosophy dictates that there should be many specialist tools and some powerful (script-based) ways of driving these tools and combining them to do specific tasks. As the number of tools increased, and variations in the way particular, well-known tools work emerged it became difficult to stitch together separate tools to perform complex jobs. In these circumstances, we'd prefer to have a single, powerful scripting language than many small tools and a simple "glue" language. Perl incorporates many of the features of the Unix shell scripting languages and individual Unix tools (such as Awk, Sed and Grep). This means that Perl has a number of powerful features built into the core language, such as regular expression searching and data structures such as hash tables and automatically resizable arrays. There are many special-purpose constructs and many individual language features borrowed from other Unix tools.

For many years Perl has been available on a diverse range of platforms including Macintosh, Windows and almost every type of Unix-like operating system. Sponsorship by Microsoft has allowed ActiveState Corporation to perform Windows integration work with Perl. The result of this is that access to parts of the Windows API and to COM/ActiveX-based applications has become straightforward.

Advantages

Disadvantages

Large, well-established user base.

Perl has innumerable inconsistencies and quirks.

There is considerable demand for good Perl programmers.

Advanced Perl code can be quite unreadable.

Perl is popular with Unix systems administrators due to the origins of the language.

The language has many implicit features that aren't apparent from the syntax. (For example expressions that evaluate differently depending on their context.) This makes Perl difficult to debug and to secure. As Tony Hoare would say "there are no obvious bugs..."

Powerful built-in text manipulation features.

Poor integration with the Java virtual machine. At present there is no JVM-based Perl interpreter.

COM, ActiveX and WSH supported on Windows.

 

Freely available on many, diverse platforms.

 

Where scripts are used to create interactive websites, many hosting services will only support Perl.

 

The Comprehensive Perl Archive Network (CPAN) provides a large, web-based library of reusable Perl code.

 

Further information can be found at http://www.perl.com/

Python

Python was originally devised by Guido van Rossum at CWI, as part of an experimental research project. The language attempts to reconcile the productivity of rapid prototyping and scripting with readability. As with Perl, Python has a number of built-in data types such as arrays and hashtables. Unlike Perl, a simple, consistent syntax makes Python code easy to maintain, and Python itself easy to learn.

A derivative project called Jython has created a Python interpreter for the Java virtual machine. Essentially this is a library of executable code that includes a Python interpreter. Jython allows Python code to call Java code and vice versa. This means that an application can be built according to the "hard-soft layers" approach: build the fixed parts of the application in Java (which runs faster but is more difficult to change) and then build the parts that we know will change out of Python (which can be changed simply by obtaining Python code as a plaintext string.) A simple way to do this, for example, would be to write Java code that reads text from a shared database. The text read from the database happens to be Python code which can be passed to Jython for execution within the same JVM. The "soft" (or adaptable) parts of the application can then be updated simply by updating the database with new Python code.

Advantages

Disadvantages

Concise, clean syntax.

Python has a smaller user base than Perl, although usage continues to increase.

The Jython interpreter allows Python code to run within the JVM. This creates low-cost opportunities for customising Java applications.

Python doesn't have the built-in regular expression operators found in Perl. This means string processing in Python involves more code.

Integrated into the Jakarta ANT build environment.

 

Sophisticated commercial products are emerging that use Python, such as the Redhat installation system, and the ZOPE publishing environment.

 

COM, ActiveX and WSH supported on Windows.

 

Freely available on many, diverse platforms.

 

Further information can be found at http://www.python.org/
 

Javascript

Javascript is a scripting language developed originally at Netscape Corporation for use in web browser and web server products. Although "Javascript" sounds like "Java", both languages are quite different. Technically there are profound differences: Java is a "strongly-typed, class-based language", whereas Javascript is "weakly-typed, prototype-based language". This means that although both languages have some superficial similarities, they are radically different. In Java, you are forced to make "class definitions" - specifications that tie down all objects created from a class. In Javascript, you cannot create new classes. As a prototype-based language, you must create new "prototypical objects", thus individual objects, although created in the same way can vary in ways that aren't allowed in Java.

Obviously Javascript has become famous due to its ubiquitous presence in the browser. As mentioned above, Microsoft has a language called Jscript that is very similar to Javascript. Differences between Javascript and Jscript led to major incompatibilities between Internet Explorer and Netscape Navigator. In order to resolve these problems, a standard (based on the Netscape language) was created, called ECMAScript (European Computer Manufacturer's Association). This language is intended to solve many of these incompatibilities.

Another problem with browser-based Javascript is that people tend to trust a stranger's browser to run code that enforces their business rules. Under such circumstances you might find that the stranger has turned off Javascript, or has a specially "cooked" implementation. (I'm aware of one recent case where business people were supposed to be locked out of systems after 30 days due to a calculation made in their browsers. In order to circumvent this all they had to do was roll the system clock on their machines back 30 days.) Huge numbers of people are familiar with Javascript, but few people really think about how to use Javascript properly.

In order to avoid security problems with browsers, by and large designers have chosen to restrict the actions that can be taken by Javascript running in the browser. However, it is possible to obtain Javascript interpreters for use in other, more useful environments. Netscape's project Rhino, for example, is a freely downloadable Javascript interpreter that runs on the Java virtual machine. Indeed, this is how the Jakarta ANT tool support for Javascript works.

Advantages

Disadvantages

Probably the most used scripting language on the planet.

Probably the most abused scripting language on the planet.

Rhino and other JVM-based interpreters allow Javascript to run within the JVM.

Few people really know how to use the full power of Javascript.

Integrated into the Jakarta ANT build environment.

Rarely available in a form that can directly manipulate items such as files and other security-sensitive things.

JScript is supported by Microsoft within WSH.

Confusion abounds about features present in different versions of Javascript, Jscript and ECMAScript. This often results in writing several different versions of the same code when dealing with Javascript in a web browser.

Due to the popularity of Javascript and the existence of ECMAScript many vendors have integrated Javascript, or closely related languages in their products. For example applications include StarOffice by Sun and Flash by Macromedia.

This language is very rarely available for writing server-side web applications, unlike both Perl and Python. (Obviously, server-side Javascript was implemented in Netscape servers.)

 

 

Details about Project Rhino can be found at http://www.mozilla.org/rhino

Other languages

There are many other scripting languages that deserve mentioning, not least:

Lisp - Perhaps the oldest scripting language ever. John McCarthy invented Lisp in the late 1950's. Perl, Python and many modern scripting languages have been influenced by the Lisp heritage. Perhaps the most obvious example is the "eval()" function present in both Python and Perl. The "eval()" function essentially takes a string, interprets it in the language and runs that string. This powerful construct allows software to do at runtime things that were not implemented or even considered when the application was originally constructed. Even the concept of using scripting to customise a large, generic application can be traced back to the use of Lisp to customise the Emacs text editor.

Tcl/Tk - A scripting language developed by John Ousterhout. The most appreciated feature of TCL/TK is the graphical "ToolKit" (TK). Ports of this toolkit are now available for Perl and Python. Critical observers will also notice a similarity between TK and the AWT framework present in Java.

Ruby - A modern scripting language developed by Yukihiro Matsumoto. This language bears many of the advantages of Python, being clean, relatively simple, and inspired by the object-oriented paradigm. Ruby borrows heavily from the Smalltalk school of language design. (See http://www.ruby-lang.org/ for details.)

_____

In this article we've discussed some of the uses of scripting and briefly summarised three mainstream scripting languages. We chose to emphasise how scripting language choice depends on many things, including your intended use of the language, your background and what platforms are important in your work.


An Industrial Revolution

The development of mass production techniques has transformed industrial life. Similarly, scripting languages provide the means to transform the life of systems administrators, software developers and IT consultants. Most of us are so busy automating things for our customers we haven't automated what we need to be really productive ourselves. Scripting languages provide a cost-effective way to automate manual, computer-based tasks so that we can concentrate on creative and profitable work.


[1] A. Hunt, D. Thomas, Ubiquitous Automation, IEEE Software, January 2002 (Also at http://www.pragmaticprogrammer.com)

[2] Ant, http://jakarta.apache.org/ant/

[3] WSH, http://msdn.microsoft.com/scripting
 

About the author

Dafydd Rees is a software developer specialising in object-technology and a graduate of the Department of Computer Science, University of Wales Swansea. Dafydd welcomes feedback on this article at http://www.dafydd.net/feedback.html

Screenshots are reprinted by permission from Microsoft Corporation.
 

Home, Services, Events, Features, Interviews, Profiles, Reviews, News, Resources, Press