Download presentation
Presentation is loading. Please wait.
1
Scripting Languages CS351 – Programming Paradigms
2
``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. By combining shell and text processing mechanisms, a scripting language can prepare input to and parse output from other processes. Consider the problem of killing the first instance of a named process.
3
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
4
Python 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.
5
A Python Program import sys, os, time def kill: if len(sys.argv) != 2 : sys.stderr.write(‘usage :’ + sys.argv[0] + ‘ prog name\n’ ) sys.exit(1) pattern=sys.argv[1] PS=os.open(“/bin/ps x -o ‘pid,command’ ”) line=PS.readline()# ignore first line line=PS.readline().rstrip()# strip out \n from the end of the line while not line == “” : if pattern in line :# jackpot proc= int( line[1:line.find(“ ”)-1] ) # extract the PID os.kill(proc, 9)# kill -9 ;) time.sleep(1) sys.exit(1)# all done line = PS.readline().rstrip() if __ name__ ==“__main__”: kill()
6
Innovate Features in Detail – Names and Scopes 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.
7
What is the scope of an undeclared variable? 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
8
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) 4 3
9
Other Scoping Rules Python cannot access a variable from a surrounding scope that is itself not global. In Tcl, the prgrammer must explicity ask for a variable from a surrounding scope. We have shown Perl’s strange scoping in earlier lectures. It has a mix of dynamic and static scope and global and local scope.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.