Scripting Languages CS 351 Programming Paradigms Glue Languages

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

Scripting Languages CS 351 – Programming Paradigms

``Glue Languages’’ As we have seen other scripting languages have been concerned with scripting

``Glue Languages’’ As we have seen other scripting languages have been concerned with scripting shell commands like bash or text processing like sed or awk. l By combining shell and text processing mechanisms, a scripting language can prepare input to and parse output from other processes. l Consider the problem of killing the first instance of a named process. l

In Bash… #!/bin/bash count=0 process=`ps x –o’pid, command’ | grep $1` for p in

In Bash… #!/bin/bash count=0 process=`ps x –o’pid, command’ | grep $1` for p in $process do if [ $count –eq 1 ] then kill $p fi count=`expr $count + 1` done

Python l l l The previous bash script is a little hacked because it

Python l l l The previous bash script is a little hacked because it will only kill the first instance of named program found. We could write a better attempt in a variety of programming languages e. g Perl, Tcl, Ruby etc. We will concentrate solely on Python however. Python was originally developed in the early 1990’s. It is now a fully fledged open source project. Python’s features include nested scopes with static scoping, higher-order functions, true iterators, lists, array slices, reflection, exception handling, multiple inheritance and provision for modules.

A Python Program import sys, os, time def kill: if len(sys. argv) != 2

A Python Program import sys, os, time def kill: if len(sys. argv) != 2 : sys. stderr. write(‘usage : ’ + sys. argv[0] + ‘ prog namen’ ) sys. exit(1) pattern=sys. argv[1] PS=os. open(“/bin/ps x -o ‘pid, command’ ”) line=PS. readline(). rstrip() while not line == “” : if pattern in line : proc= int( line[1: line. find(“ ”)-1] ) os. kill(proc, 9) time. sleep(1) sys. exit(1) line = PS. readline(). rstrip() if __ name__ ==“__main__”: kill() # ignore first line # strip out n from the end of the line # jackpot # extract the PID # kill -9 ; ) # all done

Innovate Features in Detail – Names and Scopes l l l Most scripting languages

Innovate Features in Detail – Names and Scopes l l l Most scripting languages do not require variables to be declared. Perl has an optional use strict vars mode. Due to the lack of declarations, all scripting languages use dynamic typing for variables. All values are self-descriptive, so the interpreter can perform type-checking at run time or coerce values when needed. The name and scoping conventions van vary between languages though.

What is the scope of an undeclared variable? l l l In languages with

What is the scope of an undeclared variable? l l l In languages with static scope, when we access a variable x, how do we know if it is local, global or something different? In Perl all variables are global, in PHP all variables are local. Python has some very interesting scope resolution rules. In this case, a variable is considered to be local unless it is explicitly imported. Lets look at an example from Python

E. g. Python Scope Rules i = 1; j=3 def outer() : def middle()

E. g. Python Scope Rules i = 1; j=3 def outer() : def middle() : def inner() : global i # get the global i i=4 inner() return i, j # return a tuple i=2 return middle print outer() print i, j What gets printed? (2, 3) 43

Other Scoping Rules l Python cannot access a variable from a surrounding scope that

Other Scoping Rules l Python cannot access a variable from a surrounding scope that is itself not global. l In Tcl, the prgrammer must explicity ask for a variable from a surrounding scope. l We have shown Perl’s strange scoping in earlier lectures. It has a mix of dynamic and static scope and global and local scope.