Scripting Languages - Maynooth University

Download Report

Transcript Scripting Languages - Maynooth University

Scripting Languages
CS351 – Programming Paradigms
``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.
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

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 :
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()
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





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?





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() :
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

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.