Scripting Languages


What are scripting languages?

Scripting languages are specialised programming languages that produce scripts, and have their origins in the Job Control Language (JCL), a scripting language used on IBM mainframe operating systems to tell the system how to run a batch job or start a subsystem. They are also related to the batch files used to sequentially execute a group of system commands under DOS and Windows. Scripts are text files that contain program source code rather than executable binary files in their own right. When invoked, the source code is interpreted by an interpreter. Scripting languages offer a way of quickly and easily developing and deploying web applications, both on client computers (client-side scripting) and on servers (server-side scripting), at the cost of runtime efficiency (interpreted programs tend to run significantly slower than binary executable programs).

In the "traditional" approach to client-server transactions, Web servers pass client requests to server-side scripts and receive the results of script processing via the Common Gateway Interface (CGI). The response is usually a dynamically generated Web page containing information intended to satisfy the requirements of the original query (or specifying a reason why the request cannot be satisfied). One common use of server-side scripts is to update the information in a database or retrieve the results of a database query. Today, scripting environments such as Microsoft?s ASP, Sun Microsystem?s JSP, and the open source PHP allow the direct execution of scripts, enabling them to be run by the Web server independently of the Common Gateway Interface. This usually results in lower processing overhead, and consequently better performance. As of 2008, the most widely used server-side scripting language was PHP.

Client side scripts are usually embedded within a web page, and are intended to be executed on the client computer by an interpreter built into the user's Web browser. Most client-side scripts are created using JavaScript, although other scripting languages, such as Microsoft's Jscript (closely related to JavaScript) and VBScript can also be used. As is always the case, there may be compatibility issues with different operating systems and browsers unless the coding used is strictly standards compliant. As of 2008, the most widely used client-side scripting language was JavaScript.


Active Server Pages

Microsoft's Active Server Pages (ASP) was one of the first Web application development environments to implement the direct execution of scripts, independently of the Common Gateway Interface. The scripts are embedded directly into HTML documents, which are given the extension .asp, and can contain any combination of HTML and scripting (e.g. VBScript, JScript etc.), as well as calls to components such as ActiveX controls. When a server receives a request for an ASP page, it will parse the contents of the .asp page in order to extract and process the ASP code embedded within it. ASP commands are enclosed between the delimiters <% and %>. After processing, the server will send the resulting web page (an HTML document) to the client. The example code below demonstrates how VBScript (the default scripting language) can be used with ASP to process a logon request.

<%@ LANGUAGE="VBSCRIPT" %>
<!-- FILE: login.asp -->
<HTML>
  <HEAD>
    <TITLE>Login Example</TITLE>
  </HEAD>
  <BODY>
  <%
  IF IsEmpty(Request.Form("Name")) THEN
    Response.Write "Please enter your Name"
  %>
    <FORM ACTION="login.asp" METHOD=POST>
      <INPUT NAME="Name" TYPE=TEXTBOX MAXLENGTH=20>
      <INPUT TYPE="SUBMIT" VALUE="Submit">
    </FORM>
  <%
  ELSE
    Response.Write "Welcome " & Request.Form("Name") & "!"
  END IF
  %>
  </BODY>
</HTML>

The successor to ASP is Microsoft's ASP.NET, described as a Web application framework. First released in 2002 as version 1.0, the latest version is version 3.5, released in 2007. Version 4.0 is currently in development. Officially referred to as web forms, ASP.NET pages are the building blocks of Web application development, and have the file extension .aspx. ASP.NET encourages developers to create applications using an event-driven GUI model in which pages are composed of controls. A control such as a button or label functions in much the same way as its counterpart in a standard Windows application. HTML or XHTML code and JavaScript code is generated for each control the assigns its properties and responds to its events.

From version 2.0 of ASP.NET onwards, all controls generate valid HTML or XHTML output, the detection of standards-compliant web browsers has been made more reliable, and support for Cascading Style Sheets has been improved. Although primarily designed for use on servers running Microsoft Windows Server, .NET applications can also be ported to other platforms, including Linux, Unix, Mac OS X and Solaris, thanks to Mono, an open source .NET development framework sponsored by Novell.


JavaScript

The JavaScript language was originally developed by Brendan Eich of Netscape as Mocha, later renamed to LiveScript, and finally to JavaScript. It was first publicly introduced in 1995 with version 2.0 of the Netscape browser, and enabled Web authors to embed scripts into an HTML page. Despite the name, JavaScript is not particularly closely related to Java, although both have a similar syntax and a common ancestor (the C programming language). Due to its flexibility and ease of use, Javascript was quickly adopted by Web developers, prompting Microsoft to develop their own version of JavaScript called Jscript, which was more or less compatible with JavaScript, and which was included with Internet Explorer 3.0 released in 1996. Netscape submitted JavaScript to Ecma International, an industry association dedicated to the standardisation of information and communication systems, in 1996. The result was a standardised version of the language called ECMAScript (ECMA-262). As of 2008, the latest version of the language is 1.8, a superset of the third edition of ECMAScript. The next major version will be JavaScript 2.0, which will conform to the fourth edition of ECMAScript.

JavaScript is primarily used to manipulate elements within a HTML document through the interface provided by the Document Object Model (DOM). It can set the attributes of a document element, or even create new documents and document elements dynamically. For this reason, an understanding of the DOM is essential for the effective use of JavaScript. Examples of the way in which JavaScpript can be used to provide interactivity to Web pages include opening a new browser window when the user clicks on a link in the current page, validating the contents of a form before the form data is sent to a server-side script, and changing an image as the user rolls over it with the mouse to draw the user's attention to the fact that the image may have special significance as a link (the ubiquitous rollover effect). Because the code is executed locally by the browser's built-in interpreter, the response to user actions is immediate. A trivial example of the use of JavaScript in a web page is given below.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head><title>simple page</title></head>
  <body>
    <script type="text/javascript">
      document.write('Hello World!');
    </script>
    <noscript>
      <p>Your browser does not support JavaScript, or you have JavaScript turned off.</p>
    </noscript>
  </body>
</html>

When a JavaScript-enabled browser downloads a Web page, any embedded scripts are read and interpreted by the Web browser. Functions intended to be triggered by specific events are usually defined in the head of the document and called when required by a user action. Alternatively, they can be defined in a separate file (with the extension .js) that is referenced within the head of the HTML document and downloaded by the browser together with the HTML document itself.

The Document Object Model with which JavaScript interacts is officially defined by the W3C, although in practice, browsers vary in the way they implement the model. Furthermore, not all browsers will execute JavaScript, and most browsers allow the user to disable client-side scripts. Web developers should attempt, therefore, to write standards-compliant scripts, and if possible have their script detect which browser is being used and take appropriate action if scripts are not supported by that particular browser or browser configuration.

Like any scripting language, JavaScript provides potential for misuse by hackers. For this reason it is not permitted to perform non web-related actions such as creating files on the user's hard drive. Furthermore, scripts from one site are not allowed to access to data (e.g. passwords or cookies) relating to any other site - a principle referred to as the same origin policy.


JavaServer Pages

JavaServer Pages (JSP) is a Web application development environment developed by Sun Microsystems. As with Microsoft?s Active Server Pages, JSP script elements, including Java code and consisting of Java code and certain pre-defined actions, are embedded directly into HTML documents. The resulting JSP pages are given the file extension .jsp. When requested by a client, JSP pages are compiled into Java servlets by a JSP compiler. As with the Java programming language itself, the development of JavaServer Pages is subject to the Java Community Process (JCP).


Perl

Perl is a high-level, general purpose, interpreted programming language originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987. It was originally intended as a general purpose UNIX scripting language to make the processing of reports easier. It has undergone a number of transformations since that time, and has evolved into a popular and widely-used programming language, particularly for the creation of CGI scripts. Larry Wall continues to oversee the development of the language, the latest version of which is version 5.10.8, released in December 2007. Perl is available under the GNU General Public License (GPL), and is included in the default installation of many popular operating systems. Perl version 6 is currently being developed.

Perl has been affectionately dubbed the "Swiss Army chainsaw of programming languages", due to the fact that it can be applied to so many different application areas, although it is perhaps most closely related to the C programming language. In 1991, a text called Programming Perl was published, and became the de facto reference work for Perl programmers from version 4 onwards. Perl 5 was released in 1994, and included a virtually re-written interpreter and a mechanism for extending the capabilities of the language through the inclusion of Perl modules, which allowed any sufficiently skilled and motivated Perl programmers to add new features to the language and make them available for others to use. Towards the end of 1995, the Comprehensive Perl Archive Network (CPAN) was established as a repository for Perl modules, and for the Perl programming language itself, and currently holds in excess of fifteen thousand modules by over seven thousand different authors.

Perl is one of the most popular languages used in the creation of Web applications, and is also one of the three Ps associated with the so-called LAMP solution stack (Linux, Apache, MySQL and PHP or Perl or Python) for web development. The core interpreter is written in C, but has become increasingly difficult to maintain over the years. Backward compatibility has been maintained despite the addition of many new features, and code optimisation has been undertaken at the expense of simplicity. As of early 2009, Perl 5 is still being maintained and has already assimilated some of the new features planned for Perl 6, although Perl 6 will be a major overhaul of the language and is not intended to be backwardly compatible with previous versions (there will be a compatibility mode, however).

Users of Microsoft Windows typically install one of the binary distributions of Perl available for Win32, the most popular of which is currently probably ActivePerl from ActiveState. Users of ActivePerl are largely dependent on the repackaged and precompiled modules provided by ActiveState's module repository, which are installed using the Perl Package Manager (PPM). The ActiveState repository is often less rigorously maintained than the Perl mainstream CPAN repository, and to address aspects of using Perl on the Windows platform, http://win32.perl.org was launched by Adam Kennedy on behalf of The Perl Foundation in 2006, with the aim of providing alternative Perl distributions that include an embedded C compiler and build tools to enable users to install modules from the CPAN on a Windows platform.

In Perl, the obligatory "Hello world!" CGI script may be written as follows:

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print <<EOM;
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
EOM
exit;

This CGI script, when invoked, dynamically generates a Web page with the title "Hello World!" that displays the message "Hello World!" in the browser window as a level 1 header. Perl is usually reserved for more serious uses, such as processing Structured Query Language (SQL) queries that either update a database or retrieve data from it. In Perl 5, a database-independent interface is provided between a database and an application by a Database Interface (DBI) module. A Database Driver (DBD) module provides the appropriate driver for a particular database.


PHP

PHP is a widely used server-side, cross-platform scripting language that is freely available (including the source code) under the PHP License. The initials come from the earliest version of the language developed by Rasmus Lerdorf in 1995, which was called "Personal Home Page Tools". Two Israeli developers at the Technion IIT, Zeev Suraski and Andi Gutmans, rewrote the parser in 1997, leading to the release of version 3 (now somewhat recursively referred to as PHP: Hypertext Preprocessor) in 1998. Suraski and Gutmans then rewrote PHP's core, producing the Zend Engine in 1999 which was introduced in version 4 of PHP, released in 2000 (they also founded Zend Technologies, which has its headquarters in California and a technology center in Israel). PHP 5 was released in 2004, powered by the new Zend Engine II, and included new features such as improved support for object-oriented programming, and various performance improvements. PHP is now developed and by maintained the PHP Group as a de facto standard. The current release (as of December 2008) is version 5.2.8.

PHP scripts are embedded within a Web page using the opening and closing delimiters <?php and ?>. Only code that appears within these delimiters is parsed by the PHP interpreter. Any other code (chiefly HTML) is sent to the client browser unchanged by the server. A Web page that includes PHP scripting is typically given a file extension of .php. Many of the functions provided by the library files distributed with PHP are similar to those found in the C programming language, as are the control structures used (if ... else conditional statements, and for and while loops, for example). PHP developers may write their own extensions in C to add functionality to the language which can either be compiled into PHP or loaded dynamically at runtime. The PHP Extension Community Library (PECL) provides a repository for such extensions to the PHP language. As of version 4, the PHP parser compiles input into bytecode to be processed by the Zend Engine, resulting in faster execution. Like Perl, PHP is one of the three Ps associated with the LAMP solution stack for web development.

The simple PHP code below checks which browser the client computer is using. The user agent string that the browser sends as part of its request is stored in the variable $user_agent, and is used to determine whether the client browser is Opera, Firefox or Internet Explorer (or none of these).

<?php
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
if(strpos($user_agent, 'Opera') !== false)
{
  echo ' You are using Opera';
}
elseif(strpos($user_agent, 'Gecko') !== false)
{
  echo ' You are using Firefox';
}
elseif(strpos($user_agent, 'MSIE') !== false)
{
  echo 'You are using Internet Explorer';
}
else
{
  echo 'You are using a browser other than Opera, Internet Explorer or Firefox.';
}
?>