CSC/ECE 517 Fall 2011/ch1 1g vn
Object-Oriented Languages and Scripting
Overview
Object Oriented Programming is a programming paradigm that uses abstraction to create models based on the real world. It uses several techniques from previously established paradigms, including modularity, polymorphism, and encapsulation. Today, many popular programming languages (such as Java, JavaScript, C#, C++, Python, PHP, Ruby and Objective-C) support object-oriented programming (OOP).
A scripting language, script language or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the end-user. Scripts are often interpreted from source code or bytecode, whereas the application is typically first compiled to native machine code.Scripting languages uses a lot of the features of object oriented languages.[1]
Scripting languages have their roots in the "Job Control" languages (e.g., IBM's OS JCL) that are used on "batch processing" computer systems. JCL commands tell the system to run specified programs, using particular I/O resources (e.g., files and tape drives). The "command languages" found on "time-sharing" systems (e.g., Unix and VMS) add interactive window-dressing, but perform precisely the same functions.
In recent years, object-oriented programming has become especially popular in dynamic programming languages. Python, Ruby and Groovy are dynamic languages built on OOP principles, while Perl and PHP have been adding object oriented features since Perl 5 and PHP 4, and ColdFusion since version 6. Over time, the scripting landscape has changed dramatically. Perl has added support for object orientation, Python has extended its object-oriented support, and more recently Ruby has made a name for itself as a full-fledged dynamic object-oriented scripting language with significant productivity benefits when compared to Java and C++. Groovy follows the lead of Ruby by offering these dynamic object orientation features. Not only does it enhance Java by making it scriptable, but it also provides new OO features.
The Document Object Model of HTML, XHTML, and XML documents on the Internet have bindings to the popular JavaScript/ECMAScript language. JavaScript is perhaps the best known prototype-based programming language, which employs cloning from prototypes rather than inheriting from a class. Another scripting language that takes this approach is Lua. Earlier versions of ActionScript (a partial superset of the ECMA-262 R3, otherwise known as ECMAScript) also used a prototype-based object model. Later versions of ActionScript incorporate a combination of classification and prototype-based object models based largely on the currently incomplete ECMA-262 R4 specification, which has its roots in an early JavaScript 2 Proposal. Microsoft's JScript.NET also includes a mash-up of object models based on the same proposal, and is also a superset of the ECMA-262 R3 specification.
Advantages that object orientation bring to a scripting language
Advantages that scripting capability bring to an object-oriented language
Are there any advantages to a scripting language that is not object oriented?
Is scripting essentially synonymous with dynamic typing?
Object Oriented Scripting Languages
AppleScript
AppleScript is a scripting language created by Apple Inc., through which scriptable applications and parts of Mac OS can be controlled directly. AppleScript scripting language appears to be a simple language but it is a very rich, object-oriented language, capable of performing complex programming tasks. It has the concept of classes and objects. It has some elements of OOP especially in creation of script objects and some Lisp-like natural language processing (NLP) tendencies.
AppleScript is basically an inter-application processing system, intended to exchange data between different applications and control them to automate recurring tasks.
OOPfeatures such as inheritance, instantiation, data encapsulation, and polymorphism.
Example of a simple script with one property, one handler, one nested script object, and an implicit run handler with two statements
property defaultClientName : "Mary Smith" on greetClient(nameOfClient) display dialog ("Hello " & nameOfClient & "!") end greetClient script testGreet greetClient(defaultClientName) end script run testGreet --result: "Hello Mary Smith!" greetClient("Joe Jones") --result: "Hello Joe Jones!"
Curl
Curl is a reflective object-oriented programming language for interactive web applications whose goal is to provide a effortless transition between formatting and programming. It enables embedding of complex objects in simple documents without any switch between programming languages or development platforms. Curl is a markup language like and also includes an object-oriented programming language that supports multiple inheritance.
A modern web document, which comprises of different building blocks mostly requires various kinds of methods of implementation: different languages, different tools, different frameworks and sometimes completely different teams. The biggest problem has been getting all of these blocks to communicate with each other in a consistent manner. Curl attempts to side step these problems by providing a consistent syntactic and semantic interface at all levels of web content creation: from simple HTML to complex object-oriented programming. Curl combines text markup (as in HTML), scripting (as in JavaScript), and heavy-duty computing (as in Java, C#, or C++) within one common framework. It is used in a range of internal enterprise, B2B, and B2C applications.
Curl strengths come from its having a combination of multiple inheritance, factory methods, macros and having anonymous functions that are first-class objects as well as the runtime JIT compiler compiling Curl to native code.
{Curl 5.0, 6.0, 7.0 applet} {text color = "blue", font-size = 16pt, Hello World}
Groovy
Groovy is an object-oriented programming scripting language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL. It makes testing simpler by supporting unit testing and mocking out-of-the-box. It seamlessly integrates with all existing Java classes and libraries. Groovy uses a Java-like bracket syntax. It is dynamically compiled to Java Virtual Machine (JVM) bytecode and interoperates with other Java code and libraries. Most Java code is also syntactically valid Groovy.
class Greet { def name Greet(who) { name = who[0].toUpperCase() + who[1..-1] } def salute() { println "Hello $name!" } } g = new Greet('world') // create object g.salute() // Output "Hello World!"
JavaScript
JavaScript, also known as Mocha, LiveScript, JScript, and ECMAScript, is one of the world's most popular programming languages. It is a class-free, object-oriented language, and uses prototypal inheritance instead of classical inheritance. It is a prototype-based scripting language, which is dynamic, weakly typed, and has first-class functions. It is a multi-paradigm language, which supports object-oriented, imperative, and functional programming styles. It supports OOP because it supports inheritance through prototyping as well as properties and methods. Many developers cast off JS as a suitable OOP language because they are so used to the class style of C# and Java. The primary use of JavaScript is to write functions that are embedded in or included from HTML pages and that interact with the Document Object Model (DOM) of the page. JavaScript is an excellent language to write object oriented web applications. It can support OOP because it supports Encapsulation, Polymorphism and inheritance through prototyping as well as properties and methods. Encapsulation - Support for method calls on a JavaScript object as a member of a Class. Polymorphism - The ability for two classes to respond to the same (collection of) methods. Inheritance - The ability to define the behavior of one object in terms of another by sub-classing [2]
function A() { var x = 7; this.GetX = function() { return x;} this.SetX = function(xT) { x = xT; } } obj = new A; obj2 = new A; document.write(obj.GetX() + ' ' + obj2.GetX()); obj.SetX(14); document.write(' ' + obj.GetX() + ' ' + obj2.GetX());
Lua
Lua is a powerful, fast, lightweight, embeddable scripting language. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
String Class Example
> mt = {} -- metatable > > function String(s) >> return setmetatable({ value = s or '' }, mt) >> end > > function mt.__add(a, b) >> return String(a.value .. b.value) >> end > > function mt.__mul(a, b) >> return String(string.rep(a.value, b)) >> end > > s = String('hello ') > > print(s.value) hello > print( (s + String('Lua user')).value ) -- concat 2 String instances hello Lua user > print( (s*3).value ) hello hello hello > print( (((s + String('Lua user.')))*2).value ) -- use both metamethods hello Lua user.hello Lua user.
Object Rexx
Object REXX programming language is an object-oriented scripting language initially produced by IBM for OS/2. As an object-oriented language, Rexx provides data encapsulation, polymorphism, an object class hierarchy, class-based inheritance of methods, and concurrency[3] It includes a number of useful base classes and allows you create new object classes of your own.It is a follow-on to and a significant extension of the "Classic Rexx" language originally created for the CMS component of VM/SP and later ported to MVS,[1] OS/2 and PC DOS. It is an extension of the classic Rexx language, which has been expanded to include classes (a base set of classes is supplied), objects, and methods.
factorialProgram.rex -- computes the factorial of a number
arg N . call factorial N say result exit 0 /* don't fall through to the PROCEDURE instruction */ factorial : PROCEDURE n = arg( 1 ) if n = 1 then return 1 return n * factorial( n - 1 )
Perl 5
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. There are three main terms, explained from the point of view of how Perl handles objects. The terms are object, class, and method.
- Within Perl, an object is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable.
- A class within Perl is a package that contains the corresponding methods required to create and manipulate objects.
- A method within Perl is a subroutine, defined with the package. The first argument to the method is an object reference or a package name, depending on whether the method affects the current object or the class.
Perl provides a bless() function which is used to return a reference and which becomes an object. Perl has a special variable, @ISA which governs (method) inheritance.
PHP
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Basic object-oriented programming functionality was added in PHP 3 and improved in PHP 4. Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance. In previous versions of PHP, objects were handled like value types. The drawback of this method was that the whole object was copied when a variable was assigned or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value. PHP 5 introduced private and protected member variables and methods, along with abstract classes and final classes as well as abstract methods and final methods. It also introduced a standard way of declaring constructors and destructors, similar to that of other object-oriented languages such as C++, and a standard exception handling model. Furthermore, PHP 5 added interfaces and allowed for multiple interfaces to be implemented. There are special interfaces that allow objects to interact with the runtime system. Objects implementing ArrayAccess can be used with array syntax and objects implementing Iterator or IteratorAggregate can be used with the foreach language construct. There is no virtual table feature in the engine, so static variables are bound with a name instead of a reference at compile time.
class Person { public $firstName; public $lastName; public function __construct($firstName, $lastName = '') { //Optional parameter $this->firstName = $firstName; $this->lastName = $lastName; } public function greet() { return "Hello, my name is " . $this->firstName . " " . $this->lastName . "."; } } $he = new Person('John', 'Smith'); echo $he->greet(); // prints "Hello, my name is John Smith."
Python
Python is a remarkably powerful dynamic programming language that is used in a wide variety of application domains. Python is often compared to Tcl, Perl, Ruby, Scheme or Java. Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. The class statement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming. Python allows programmers to define their own types using classes, which are most often used for object-oriented programming.
Ruby
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Matsumoto has stated, "I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language".[9] Ruby is object-oriented: every data type is an object, including classes and types that many other languages designate as primitives (such as integers, booleans, and "nil"). Variables always hold references to objects. Every function is a method and methods are always called on an object. Methods defined at the top level scope become members of the Object class. Since this class is an ancestor of every other class, such methods can be called on any object. They are also visible in all scopes, effectively serving as "global" procedures. Ruby supports inheritance with dynamic dispatch, mixins and singleton methods (belonging to, and defined for, a single instance rather than being defined on the class). Though Ruby does not support multiple inheritance, classes can import modules as mixins.
class Person attr_reader :name, :age def initialize(name, age) @name, @age = name, age end def <=>(person) # Comparison operator for sorting @age <=> person.age end def to_s "#@name (#@age)" end end group = [ Person.new("Bob", 33), Person.new("Chris", 16), Person.new("Ash", 23) ] puts group.sort.reverse
Tcl-Tk
Tcl (originally from "Tool Command Language", but conventionally rendered as "Tcl" rather than "TCL"; pronounced as "tickle" or "tee-see-ell") is a scripting language created by John Ousterhout. It is commonly used for rapid prototyping, scripted applications, GUIs and testing. Tcl is used on embedded systems platforms, both in its full form and in several other small-footprinted versions. The combination of Tcl and the Tk GUI toolkit is referred to as Tcl/Tk.
oo::class create fruit { method eat {} { puts "yummy!" } } oo::class create banana { superclass fruit constructor {} { my variable peeled set peeled 0 } method peel {} { my variable peeled set peeled 1 puts "skin now off" } method edible? {} { my variable peeled return $peeled } method eat {} { if {![my edible?]} { my peel } next } } set b [banana new] $b eat → prints "skin now off" and "yummy!" fruit destroy $b eat → error "unknown command"
Conclusion
References
[1]https://developer.mozilla.org/En/Introduction_to_Object-Oriented_JavaScript