220 likes | 728 Views
Scripting Languages. CS351 – Programming Paradigms. Common Characteristics of Scripting languages. 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.
E N D
Scripting Languages CS351 – Programming Paradigms
Common Characteristics of Scripting languages. • 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 • Perl has a JIT compiler that reads the entire program before execution. • Other languages are quite happy to only execute a line as soon as it has been read. • Python, Tcl and Ruby will also accept commands from the keyboard.
2 – Economy of Expression • To support rapid development and interactive use, scripting languages require very little boilerplate code. • This can be illustrated very clearly by the following example: public static void main(String args[]) { System.out.println(“Hello!”); } print “Hello!\n” • Another example is reading a file word by word.
3 – Lack of Declarations and simple scoping rules • 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. • In PHP, Ruby and Python, the type of a variable is checked immediately prior to use. • In others such as Tcl and Perl, a variable can be interpreted differently in different contexts. • E.g. $a = “4”; print $a . 3 . “\n”; print $a + 4 . “\n”;
5 – Easy access to other programs • Most programming languages provide a way to interact with the underlying commands of the OS. • However this can be quite convoluted, as anyone who has had to do work with the Runtime.exec command knows. • Scripting languages support these OS commands and their outputs much more cleanly and directly. • Perl provides 100 built-in commands for the OS while the os module in Python is an excellent library for executing commands.
6 – Pattern Matching • 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 • High level data types such as sets, bags, dictionaries, lists and tuples are some of the very convenient features provided by scripting languages. • 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. • E.g. in Python a dictionary can map a key to a value: 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 freedom from working within an ISO published standard, scripting languages have been at the recent forefront of programming language development. • Most scripting languages have had only a single person driving the initial development. • All of the most interesting features and powerful features appear in Python: • E.g. true iterators, array slices, multiway assignment, anonymous first-class functions and functional type lists.
Problem Domains for Scripting Languages • 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: • Shell Languages • Text Processing • Glue Languages
Shell Languages • 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… • Some features provided by a shell languages. • List all file ending in .txt -> ls *.txt • This feature is known as filename expansion or globbing. • There are other alternatives to this command: • ls fig?.eps • ls fig[0-9].eps • ls fig3.{eps,pdf} • What do each of these commands do?
Shell Languages cont… • Such simple commands do not even hint at the flexibility provided by shell languages. • It is possible to provide loops within shell languages. • These can be very useful. • E.g. for file in *.eps do ps2pdf $file done • What is going on here?
Shell Languages cont… • We can also use conditionals within our bash scripts: for file in *.eps do if [ ! test –e ${file%.eps}.pdf ] then ps2pdf $file fi done • The test command has many features, type man test to get a list of them all.
Shell Languages cont… • 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? • It is is also possible to send output to and from files using re-directs. ls *.txt > files for file in *.txt; do echo $file >> files; done • What do these commands do?
Shell Languages cont… • There are some simple rules to bear in mind when using shell scripts. foo=bar single=‘$foo’ double=“$foo” echo $single $double • What is printed? • When using other programs in a shell script, they must be placed within backticks. bname=`basename $file .txt`
Text Processing • 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. • Very powerful and can be used to write complex scripts but is more commonly used for one-line programs. • E.g. sed –e ‘/^[[:space:]]*$/d’ • 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 both the shell languages and the text processing languages. • Imagine writing a script to kill a named process in *nix. • We could do it ( hack it ) via a bash script or we could use a general purpose language to do it. • We will discuss Python in detail on Thursday.