Scripting Languages CS 351 Programming Paradigms Common Characteristics

  • Slides: 20
Download presentation
Scripting Languages CS 351 – Programming Paradigms

Scripting Languages CS 351 – Programming Paradigms

Common Characteristics of Scripting languages. 1. 2. 3. 4. 5. 6. 7. Both Batch

Common Characteristics of Scripting languages. 1. 2. 3. 4. 5. 6. 7. Both Batch and Interactive use. Economy of Expression. Lack of declarations; simple scoping rules Flexible dynamic typing. Easy access to other programs. Sophisticated Pattern matching. High-level data types.

1 – Batch and Interactive Use l Perl has a JIT compiler that reads

1 – Batch and Interactive Use l Perl has a JIT compiler that reads the entire program before execution. l Other languages are quite happy to only execute a line as soon as it has been read. l Python, Tcl and Ruby will also accept commands from the keyboard.

2 – Economy of Expression To support rapid development and interactive use, scripting languages

2 – Economy of Expression To support rapid development and interactive use, scripting languages require very little boilerplate code. l This can be illustrated very clearly by the following example: l public static void main(String args[]) { System. out. println(“Hello!”); } print “Hello!n” l Another example is reading a file word by word.

3 – Lack of Declarations and simple scoping rules l l l Most scripting

3 – Lack of Declarations and simple scoping rules l l l Most scripting languages dispense with declarations. They also use simple rules to govern the scopes of names ( identifiers ). In Perl every name is global by default. In Tcl and PHP, everything is local by default. Python has a unique implementation in that any variable that is assigned a value is local to the block in which the assignment appears. Special syntax is required to access the variable from a surrounding scope.

4 – Flexible Dynamic Typing Most scripting languages are dynamically typed. l In PHP,

4 – Flexible Dynamic Typing Most scripting languages are dynamically typed. l In PHP, Ruby and Python, the type of a variable is checked immediately prior to use. l In others such as Tcl and Perl, a variable can be interpreted differently in different contexts. l E. g. $a = “ 4”; print $a. 3. “n”; print $a + 4. “n”; l

5 – Easy access to other programs Most programming languages provide a way to

5 – Easy access to other programs Most programming languages provide a way to interact with the underlying commands of the OS. l However this can be quite convoluted, as anyone who has had to do work with the Runtime. exec command knows. l Scripting languages support these OS commands and their outputs much more cleanly and directly. l Perl provides 100 built-in commands for the OS while the os module in Python is an excellent library for executing commands. l

6 – Pattern Matching l l l As scripting language ancestors were used for

6 – Pattern Matching l l l As scripting language ancestors were used for text processing, the support for pattern matching in text files is impressive. Scripting languages have very good facilities for pattern matching, searching and string manipulation. This is all based upon the notion of extended regular expressions. sed is a very powerful scripting language for text processing. What does sed '1!G; h; $!d' do?

7 – High level data types such as sets, bags, dictionaries, lists and tuples

7 – High level data types such as sets, bags, dictionaries, lists and tuples are some of the very convenient features provided by scripting languages. l In C++, it is possible to use ``operator overloading’’ to create advanced user-defined types but scripting languages go one step further by building high-level types into the syntax and semantics. l E. g. in Python a dictionary can map a key to a value: l dict([(x, x**2) for x in (2, 4, 6)]) {2: 4, 4: 16, 6: 36}

Scripting Languages Evolution Due to the dynamism of the open source community and the

Scripting Languages Evolution Due to the dynamism of the open source community and the freedom from working within an ISO published standard, scripting languages have been at the recent forefront of programming language development. l Most scripting languages have had only a single person driving the initial development. l All of the most interesting features and powerful features appear in Python: l E. g. true iterators, array slices, multiway assignment, anonymous first-class functions and functional type lists. l

Problem Domains for Scripting Languages l l l Some languages such as Perl, Python

Problem Domains for Scripting Languages l l l Some languages such as Perl, Python and Ruby are intended for general purpose use as they support features such as modules, separate compilation, reflection etc. However a lot of scripting languages are intended for use within a well defined domain. We will look in detail at the following: 1. 2. 3. Shell Languages Text Processing Glue Languages

Shell Languages l l In the early 1970’s a command language for automating the

Shell Languages l l In the early 1970’s a command language for automating the control of Unix programs was written. This was known as ``shell’’ or sh. This was extended to allow for variables and control flow and was known as bash. This is the default Unix command shell. Shell languages allow for many operations including manipulating file names, arguments and commands and for tying other programs together.

Shell Languages cont… l Some features provided by a shell languages. l List all

Shell Languages cont… l Some features provided by a shell languages. l List all file ending in. txt -> ls *. txt This feature is known as filename expansion or globbing. l There are other alternatives to this command: l l ls fig? . eps ls fig[0 -9]. eps ls fig 3. {eps, pdf} What do each of these commands do?

Shell Languages cont… Such simple commands do not even hint at the flexibility provided

Shell Languages cont… Such simple commands do not even hint at the flexibility provided by shell languages. l It is possible to provide loops within shell languages. l These can be very useful. l E. g. l for file in *. eps do ps 2 pdf $file done l What is going on here?

Shell Languages cont… l We can also use conditionals within our bash scripts: for

Shell Languages cont… l We can also use conditionals within our bash scripts: for file in *. eps do if [ ! test –e ${file%. eps}. pdf ] then ps 2 pdf $file fi done l The test command has many features, type man test to get a list of them all.

Shell Languages cont… l It is possible to pass the results of one program

Shell Languages cont… l It is possible to pass the results of one program as the input to another very easily through the use of pipes. find. –name *. txt | ls | wc –l What does this do? l It is is also possible to send output to and from files using redirects. l ls *. txt > files for file in *. txt; do echo $file >> files; done l What do these commands do?

Shell Languages cont… l There are some simple rules to bear in mind when

Shell Languages cont… l There are some simple rules to bear in mind when using shell scripts. foo=bar single=‘$foo’ double=“$foo” echo $single $double l l What is printed? When using other programs in a shell script, they must be placed within backticks. bname=`basename $file. txt`

Text Processing l l l Shell languages are heavily string oriented. Commands are strings

Text Processing l l l Shell languages are heavily string oriented. Commands are strings parsed into single words. All variables are string-valued and there are elaborate quoting conventions. However shell languages cannot be used like a text editor. How can we account for interactive features such as insertion, deletion, replacement, bracket-matching etc. Through the use of special ``online’’ editors such as sed and awk.

sed Stands for stream editor. l Very powerful and can be used to write

sed Stands for stream editor. l Very powerful and can be used to write complex scripts but is more commonly used for one-line programs. l E. g. l sed –e ‘/^[[: space: ]]*$/d’ l awk is an attempt to modify sed to look more like a programming language.

Glue Languages These general purpose scripting languages inherit a rich set of features form

Glue Languages These general purpose scripting languages inherit a rich set of features form both the shell languages and the text processing languages. l Imagine writing a script to kill a named process in *nix. l We could do it ( hack it ) via a bash script or we could use a general purpose language to do it. l We will discuss Python in detail on Thursday. l