5. Scripting Languages¶
1 Lecture Notes¶
1 Scripting Languages¶
- The first scripting language was the JCL or Job Control Language on IBM mainframes that was developed back in the 1950’s.
- Can be Interpreted or Compiled, but mostly interpreted.
- Scripting Languages: PHP, Python, Perl, Ruby, JavaScript, VBScript, Tcl, Lua, Bash, C#.
- Advantages:
- Flexibility, rapid development, and dynamic typing.
- The languages can pe specialized in little areas, thus we have groups of scripting languages that does similar jobs.
- Some of them Work as
Glue
languages, they can be used to connect different programs together. - Support high level data types, like dictionaries, lists, sets, and dynamic arrays easily.
- Two groups of scripting languages:
- Execute terminal commands in batch mode: bash, .bat.
- Text processing languages: sed, awk => Perl(Report supporting languages), Python, Ruby, TCL, PHP, JS, VBScript.
- Batch Jobs are jobs that run without user interactivity, they just run and finish; like bash scripts. as opposed to Interactive Jobs that require user interaction (like a GUI or web page).
- Characteristics of scripting languages:
- Both Batch and Interactive use.
- Dynamic typing: no need to declare variables, simple scoping rules.
- Can access external programs easily.
- Sophisticated string processing (regular expressions) and pattern matching.
- High level data types: lists, dictionaries, sets, dynamic arrays.
- GUI scripting happens using Macros.
1 Perl¶
- Developed by Larry Wall in 1987 as general purpose scripting language for report processing.
- Advantages:
- Perl is really good at string processing.
- Perl is popular for CGI (Common Gateway Interface) scripts.
- Perl works well with Unix, and it is used for system administration.
- Perl programs are usually vert short.
- Disadvantages:
- Perl is attached to Unix, and it is not portable (although it is available for other platforms, it is not that good).
- Perl does not scale well in big programs.
- Weak subroutines.
- Heavy use of global(special) variables
- Syntax is not that good.
- Characteristics:
- Perl is Interpreted.
- Runs on a server, similar to PHP and Python.
- Perl is Dynamically Typed (No need to declare types, types are inferred) and Weakly Typed(Can reassign the variable with different type).
- Perl variable binding happens at runtime (execution time) and can be either static(persistent) or dynamic(limited lifetime).
- Syntax:
- Case sensitive.
- The first comment tells the system where to find the Perl interpreter (usually
/usr/bin/perl
). - Variables are prefixed with
$
. - The keyword
my
is used to declare variables. - Supports control structures like:
if, else, elsif, while, for, do while
.
1 Python¶
- Developed by Guido van Rossum in 1991.
- Can be compiled to bytecode and run on a virtual machine.
- Python is a high-level, interpreted, interactive and object-oriented scripting language.
#!/usr/bin/perl
#
# This is a comment
#
print "Hello World\n";
1 Regular Expressions¶
- Regular expressions are a language for specifying text patterns as it has grammar, syntax, structure, and semantics.
- Applications of REs:
- Process natural language.
- Search for virus signatures.
- Searching for patterns in text files, web pages, etc.
- Search and replace in text files.
- Filter spam text.
- Validate input.
- Search for patterns in DNA sequences.
- Crawling the web.
- Documentation generation (e.g. javadoc).
- syntax:
+
means one or more.[]
means any character in the brackets.^
means not.[^10]
means any character except 1 and 0, while[10]
means any character 1 or 0.()
means grouping..
means any character.\
means escape character.*
means zero or more.\d
means any digit.\D
means any non-digit.
2 Scripting: Higher Level Programming for the 21st Century¶
System Programming Languages And Assembly¶
- Assembly language programs explicitly represent the machine state, that is, the state of every register and memory location.
- Higher-level languages like: Lisp, Fortran, Algol built on top of Assembly (around 1950s), and the program instructions no longer represent the machine state, however, a Compiler translates those instructions to machine code.
- Later, More higher level languages like: Pascal, PL/I, C, C++, Java are build on top of Algol which we call them System Programming Languages (around 1970s).
- Higher Level:
- The programmer writes less code as most of the low-level details are generated by the compiler.
- Register allocations are done by the compiler, no need to specify which register to use.
- Procedure calling sequences are generated by the compiler, no need to manually move arguments between registers and stack.
- Simple Keywords like
if, while, for
are used instead of GOTO statements.
- On average, each line of a system programming language corresponds to 5 machine instructions While assembly instructions correspond to 1 machine instruction.
- Modern computers are typeless which means Assembly is also typelss, however, System Programming Languages are strongly typed.
- Code and Data are septate in System Languages, which makes it hard to generate code at runtime.
Scripting Languages¶
- Scripting languages as opposed to System Programming Languages(c, C++, Java).
- Scripting Languages assumes that a useful collection of components already exist in other languages (usually lower level), and the scripting language would not write programs from scratch, but rather combining those existing components.
- Scripting languages are rarely used to write core algorithms, however, they used to glue together components written in other System languages.
- Scripting languages Data and code are interchangeable, which makes it easy to generate code at runtime, or generate programs that generate other programs on the fly.
- Scripting languages are typeless.
- Scripting languages are just as safe as system languages.
- Scripting languages are interpreted.
- Scripting languages are less efficient than system languages: system languages run 10-20 times faster because of less runtime checks (most of the checks are done at compile time), and less overhead (no need to parse the code at runtime).
- Scripting languages are higher level than system languages, which means a single statement generates more machine instructions than a single statement in a system language (Ratio is 100s-1000s/1s-10s respectively).
- Scripting languages supports faster development, 5-§0 times faster than system languages for the same task.
- Scripting languages provided less benefits in terms of development time when they used to write program for the first time; however, they are extremely beneficial for program rewrites that are written in system languages for the first time.
Scripting Languages Applications¶
- Scripting languages applications: GUI, Web, Component Frameworks.
- GUI:
- System languages GUIs are hard to write, learn, maintain, and debug.
- Scripting languages GUIs: Visual Basic, HyperCard, Tcl/TK.
- Internet: Perl for CGI (Common Gateway Interface) scripts, JavaScript for client-side scripting, PHP for server-side scripting.
- Component Frameworks: ActiveX, JavaBeans, CORBA.
- Why scripting languages are popular:
- The popularity of the three applications mentioned above (GUI, Web, Component Frameworks).
- Scripting languages have evolved to be more powerful and easier to use.
- Programmers are casual and non-professional these days, as opposed to the more more sophisticated programmers in the past who worked in large teams and wrote large programs.
- The decrease of love towards OOP, which most system languages are based on, and scripting languages are not.
3 Competitors to Java: Scripting languages¶
- The main advantage of scripting over general-purpose programming is cost. Development time is today usually more expensive than fast hardware and memory. Scripting languages are seen as mostly easier to learn, and simpler and faster to use.
TCL/TK¶
- TCL is basic scripting language, while TK is a GUI toolkit.
- Tcl can be used in internet scripting, or batch jobs (like bash).
- The example below is a stopwatch program written in TCL/TK:
set seconds 0.0; set stopped 1
label .stopwatch_display -text $seconds
button .start -text Start -command {
if $stopped {
set stopped 0
tick
}
}
button .stop -text Stop -command {set stopped 1}
pack .stopwatch_display -side bottom -fill both
pack .start .stop -side left
proc tick { } {
global second stopped
if $stopped return
after 100 tick
set seconds [expr $seconds + .1]
.stopwatch_display config -text [format “%.1f” $seconds]
}
Perl¶
- Started as Unix system administration language, then it used in the web to develop GCI scripts.
- Perl is the duct tape of the internet.
- Perl + fast-CGI + DBI is probably the most widely used database connector on the world wide web at the moment (1998).
- Perl has a good support for regular expressions.
- The example below shows TCP/IP ps daemon written in Perl:
use strict;
use Socket;
my $port = 2001;
my $porto = getprotobyname `tcp`;
my $ps = `usr/ucb/ps`
socket SERVER, PF_INET, SOCK_STREAM, $porto or die "socket: $!";
bind SERVER, sockaddr_in($port, INADDR_ANY) or die "bind: $!";
listen SERVER, SOMAXCONN or die "listen: $!";
print "Server $0 started on port $port\n";
for(;;){
accept CLIENT, SERVER;
print CLIENT `$ps`;
close CLIENT;
}
4 An overview of scripting languages¶
- Characteristics of scripting languages:
- Interpreted or bytecode interpreted, but not compiled.
- Garbage collected: memory management is done automatically and not by the programmer.
- High-Level: that includes high level data structures such as lists, dictionaries, and sets.
- The execution environment is integrated with the program itself.
- Can access modules written in lower-level languages (C, C++, Java).
- Application Areas:
- Command scripting languages (1960s):
- JCL, shell, awk, sed, perl.
- Regular expressions and Job Control Language.
- Application scripting languages (1980s):
- Visual Basic (for Microsoft Apps): GUI and component embedding. Replaced older office scripting languages (WordBasic, ExcelBasic, etc.). Mostly used with Microsoft Office.
- VBScript (for Internet Explorer): Client-side scripting for web pages.
- LotusScript (for Lotus Notes).
- JavaScript (for Netscape Navigator).
- Markup languages:
- GML (for IBM mainframes) was the first markup language in 1960s.
- Tex (for UNIX): 1979. Popular for scientific documents and formulas.
- HTML, XML.
- Universal scripting languages:
- Perl (Practical Extraction and Report Language) (for UNIX): 1987. Used for system administration, CGI scripts, and web applications.
- Command scripting languages (1960s):
5 Beginner’s Introduction to Perl¶
- Functions are the commands that interpreter runs.
- Scalar(Primitive) types: Numbers, and Strings and References. start with $.
- Arrays: lists of scalars. start with @.
- Hashes: associative arrays. start with %.
print "Hello, world!\n", "How are you?\n"; # no parentheses for function calls
print 'Hello, world!\n', # single quotes to print literally 'Hello, world!\n'
print "Hello, world!\n", # double quotes to print the string and a new line
$i = 5; # $i is a scalar variable `Number`
$s = "i is $i"; # $s is a scalar variable `String`
@arr = (1, 2, 3); # @arr is an array of numbers, accessed as $arr[0], $arr[1], $arr[2]
%hash = ('a' => 1, 'b' => 2); # %hash is a hash of numbers, accessed as $hash{'a'}, $hash{'b'}
foreach $i (@arr) { # foreach loop
print $i, "\n";
}
while ($i < 10) { # while loop
print $i, "\n";
$i++;
}
for ($i = 0; $i < 10; $i++) { # for loop
print $i, "\n";
}
$min = 1;
$max = 10;
for ($min .. $max) { # for loop
print $_, "\n"; # $_ is the default variable, loops from 1 to 10
}
for (keys %hash) { # for loop
print %hash{$_}, "\n"; # $_ is the default variable, loops over the keys of %hash, and prints the values
}
6 Instant Python¶
- Python passes arguments by reference for mutable objects, and by value for immutable objects (numbers, strings, tuples).
- Logical falsy values:
False
,None
,0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
,[]
,()
,{}
,set()
,range(0)
. - Python allows multiple class inheritance, like
class Derived(Base1, Base2, Base3):
. - The comment
#!/usr/bin/env python
at the beginning of the script tells the shell to run the script with the Python interpreter. - The
if __name__ == '__main__': main()
statement is used to run the script as a program, and not as a module. That is, if the module is not imported in another script or module, then run the main function.
x,y,z = 1,2,3 # multiple assignment
x,y = y,x # swap x and y
a,b,*rest = [1,2,3,4] # a=1, b=2, rest=[3,4]
a=b=14 # multiple assignment
if(x < 20 and 10 < x <20): # (x < 20) and (x < 10 and x < 20)
print("x is between 10 and 20")
for (i in range(10)): # for loop
print(i)
l = [1,2,3,4,5] # list
sl = l[1:3] # slice, sl=[2,3]
sl = l[1:] # sl=[2,3,4,5]
d = {'a':1, 'b':2} # dictionary
d['c'] = 3 # add a new key-value pair
def f(x): # function
return x*x
class C: # class
def __init__(self, x): # constructor
self.x = x
def f(self): # method
return self.x*self.x
def moreArgs(self, x, y, z): # method
return x+y+z
c = C(10) # create an instance of C, self is passed implicitly
c.f() # call method f, self is passed implicitly
c.moreArgs(1,2,3) # call method moreArgs, self is passed implicitly
print (a or b) # a if a is truthy print it, otherwise print b
a = {}
b = 2
print (a or b) # prints 2
class Derived(Base1, Base2, Base3): # multiple inheritance
def __init__(self, x, y, z): # constructor
Base1.__init__(self, x) # call Base1 constructor
Base2.__init__(self, y) # call Base2 constructor
Base3.__init__(self, z) # call Base3 constructor
def ExpectException(exception, func, *args, **kwargs): # function
try:
func(*args, **kwargs)
except exception:
return True
return False
def main(): # main function
print("Hello, world!")
if __name__ == '__main__':
main() # run main function
7 Which language is right for you?¶
- You can do any thing with Perl, PHP, Python, Rexx, Scheme, Tcl, and other such languages:
- Script dynamic Webpages, build administrative utilities, prototype graphical user interface applications, glue together legacy data and processes, and so on.
- You can code in procedural, object-oriented, or functional styles.
- Run on Windows, Macintosh, Unix systems, Web, and almost any other platform.
- You can manage other processes, and script Java and COM objects.
- You have limited access to hardware such as serial ports, but you can extend built-in capabilities with C or other languages.
- To Choose a language for a new project:
- Pick the language that you are most comfortable with, and that you can get the job done with.
- Pick a language that has a large community, conferences, and books to support it.
8 9 JavaScript¶
- JavaScript is an interpreted object based, statically scoped, and dynamically typed language.
- It has no local constants. All variables and functions(subprograms) are properties (attributes) of some object. Some of these are read only.
- It has automatic garbage collection.
- Each function call creates a new execution context (global environment) that includes an explicit local activation record called the “call object”.
- The different execution objects are themselves properties of a global object and can be referred to.
- The
this
keyword refers to the call object. - The function arguments are stored in an array-like object called
arguments
that has:arguments.length
property.arguments.callee
property that refers to the function itself.arguments.caller
property that refers to the arguments of the function that called the function.arguments.arity
deprecated, usearguments.length
instead.arguments.caller.callee
refers to the function that called the function.
function x(z) {
console.log(arguments, arguments.callee, arguments.caller, arguments.arity);
}
x(1); // [1] ƒ x(z) undefined 1
10 Regular Expressions¶
- Regular expressions are combinations of special characters and symbols used for pattern matching.
- Special ReGex:
- Ordinary alphanumeric characters match themselves.
[a-zA-Z0-9]
- Symbols:
+
one or more,*
zero or more,?
zero or one,.
any character,^
beginning of line,$
end of line,|
or,[]
character class,()
grouping,\
escape.
- Ordinary alphanumeric characters match themselves.
- Examples:
^Word
matches any line that starts withWord
.\([a-zA-Z]+\)
matches any word (s) in parentheses.
11 Linux Shell Scripting¶
- This is a very long Linux tutorial; can be referenced when needed.
12 PHP¶
- This is a very long PHP tutorial; can be referenced when needed.
References¶
-
UoPeople. (2023). CS4402: Comparative Programming Languages. Lecture Notes Unit 5. ↩↩↩↩↩
-
Scripting: Higher Level Programming for the 21st Century by John K. Ousterhout: http://www.stanford.edu/~ouster/cgi-bin/papers/scripting.pdf ↩
-
Competitors to Java: Scripting languages by Judith Bishop and Riaan Hurter, Computer Science Department, University of Pretoria: https://my.uopeople.edu/pluginfile.php/1709787/mod_book/chapter/441657/CompetitorsToJava.pdf ↩
-
An overview of scripting languages by Alexander Kanavin, University of Technology, Finland: http://www.sensi.org/~ak/impit/studies/report.pdf ↩
-
Beginner’s Introduction to Perl by Doug Sheppard available at http://www.perl.com/pub/2000/10/begperl1.html ↩
-
Instant Python by Magnus Lie Hetland available at http://hetland.org/writing/instant-python.html ↩
-
Which Language is right for you? By Cameron Laird and Kathryn Soraiz, Unix Insider. https://www.itworld.com/article/2799348/development/which-language-is-right-for-you-.html ↩
-
JavaScript Introduction: This site provides a good overview of the syntax and structure of the JavaScript language and is available at http://csci.csusb.edu/dick/samples/javascript.html ↩
-
Examples from JavaScript: The Definitive Guide By David Flanagan, Copyright © 1997-2000, O’Reilly & Associates available at http://examples.oreilly.com/9781565922341/ ↩
-
Regular Expressions for Beginners by Yasumasa Someya available at http://www.someya-net.com/concordancer/eng-quick_regex.html ↩
-
Linux Shell Scripting Tutorial v2.0 by Vivek Gite and contributors available at: http://bash.cyberciti.biz/guide/Main_Page ↩
-
PHP Essentials by Neil Smyth available at: http://techotopia.com/index.php/PHP_Essentials ↩