CSCI 553: Networking III Unix Network Programming Spring 2006 PowerPoint PPT Presentation

presentation player overlay
1 / 26
About This Presentation
Transcript and Presenter's Notes

Title: CSCI 553: Networking III Unix Network Programming Spring 2006


1
CSCI 553 Networking III Unix Network
ProgrammingSpring 2006
  • High Level Scripting Languages
  • (Perl Python)

2
Why HL Scripting Languages?
  • 2 Factors determine time to solution
  • How long it takes to write a program (human time)
  • How long it takes that program to run (machine
    time)
  • Different languages make different tradeoffs
    between programming speed and execution speed.
  • Programmers write the same number of lines of
    code per day no matter the language

3
Machine Speed vs. Human Speed
Machine Speed
Fortran
C
C
Java
C
Matlab
Shell Scripts
Python
Perl
Human Speed
4
High Level Scripting Languages are for
  • Solving problems with the least effort
  • Gluing together existing pieces (written in
    shell, compiled languages, system commands, etc.)
  • Providing better access to system features than
    shell
  • Fast prototyping, quick turn around

5
Perl
  • Practical Extraction Report Language
  • Shell scripts not enough and C programs were
    overkill
  • Originally targeted at generating reports and
    other text-oriented functions
  • Similar syntax to C and shell, elements taken
    from both
  • Perl is an interpreted language
  • Now a days, probably already available on any
    Unix/Linux box you come across
  • If not easily downloadable and installable
    http//www.perl.com

6
Perl Basics
  • You can run perl interactively, or from a script
    file
  • Interactive perl de 0
  • File The normal method, make executable and use
    ! syntax in Linux/Unix
  • Hello World

dharter_at_nisl labs cat hello.pl !/usr/bin/perl
print Hello world.\n dharter_at_nisl labs
chmod ux hello.pl dharter_at_nisl labs
./hello.pl Hello world.
7
Perl Variables, Strings and Integers/Floats
  • Variables not explicitly types (unlike C but like
    shell scripts)
  • always needed
  • i 3
  • print i
  • All typical mathematical operations are available
  • res 1 3 5
  • Strings are easy to define and manipulate
  • i A . B concatenation operator
  • print i, \n

8
Perl Arrays
  • sign for single value variables, _at_ used to
    declare and reference arrays
  • _at_arr (1,2,3,4,5)
  • print _at_arr0, \n
  • print _at_arr, \n prints all elements no
    spaces

9
Associative Arrays
  • HL Scripting languages add associative arrays as
    a basic type, like the array
  • known as a Hash, or dictionary
  • Not in basic C/C and other compiled languages
  • Is in the java language as a Dictionary
  • Probably the most useful underused basic type

10
Perl Associative Arrays
  • DBlt23gt month ("January", 1, "February", 2,
    "March", 3)
  • DBlt24gt print _at_month'January'
  • 1
  • DBlt25gt print _at_month'March'
  • 3
  • DBlt26gt print keys(month)
  • FebruaryMarchJanuary
  • DBlt27gt _at_monthnames keys(month)
  • DBlt29gt print _at_monthnames0
  • February
  • DBlt30gt print _at_monthnames1
  • March

11
Perl String Operators
  • Concatenation uses .

DBlt31gt firstname "Derek" DBlt32gt lastname
"Harter" DBlt33gt fullname firstname . " " .
lastname DBlt34gt print fullname Derek Harter
  • Simple matching operations for string
  • search in string
  • DBlt37gt print fullname "ere"
  • 1
  • string substitution
  • DBlt38gt fullname s/ere/fsf/
  • DBlt39gt print fullname
  • Dfsfk Harter

12
Perl Control Structures
for ( i 0 i lt 10 i ) print i,
print \n
i 0 if ( i 0 ) print its
true\n else print its false\n
foreach n (1..15) print i, print
\n
while ( i 0) print its true\n
i
13
Perl File I/O
while (_at_lineltstdingt) foreach i (_at_line)
print -gt, i
FILE info.dat open (FILE) _at_array
ltFILEgt close (FILE) foreach line (_at_array)
print line
14
Perl Functions
  • Complicated syntax, looks like bash/Korn functions

sub pounds2dollars EXCHANGE_RATE 1.54
pounds _0 refers to the first parameter
passed to function return (EXCHANGE_RATE
pounds) book 3.0 value of book in
British pounds value pounds2dollars(book) pri
nt Value in dollars value\n
15
Python
  • (much?) easier to read than Perl
  • All of Perls features, but cleaner syntax
  • Especially defining functions
  • OO support built in from start

16
Python Basics
  • You can run python interactively or from a script
    file
  • Interactive python
  • File same as usual, make executable and add
    !/usr/bin/python
  • Hello World
  • No needed for statement terminator
  • Spacing used for level/grouping

dharter_at_nisl labs chmod ux hello.py dharter_at_n
isl labs cat hello.py !/usr/bin/python print
"Hello world.\n" dharter_at_nisl labs
./hello.py Hello world.
17
Python Variables, Strings and Integers/Floats
  • Variables not explicitly typed (unlike C but like
    shell scripts, same as perl)
  • but, no special identifier needed for variable
    names
  • i 3
  • print i, i
  • All typical mathematical operations are available
  • gtgtgt res 1 3 5
  • gtgtgt print res
  • 16
  • gtgtgt print 10 2
  • 100
  • Strings are easy to define and manipulate
  • gtgtgt i "Hello" " " "World"
  • gtgtgt print i
  • Hello World

18
Python Arrays
  • Again no special syntax to distinguish arrays
  • Referred to as general Lists in Python
  • A mutable sequence of objects, can grow and
    shrink
  • gtgtgt x1,2,3,4,5
  • gtgtgt print x0
  • 1
  • gtgtgt print x
  • 1, 2, 3, 4, 5
  • gtgtgt print len(x)
  • 5
  • Slices are easy to do in python
  • gtgtgt print x34
  • 4
  • gtgtgt print x2
  • 1, 2

19
Python Associative Arrays
  • Python fully supports Associative Arrays, usually
    referred to as Dictionaries in the Python
    literature
  • gtgtgt month "January" 1, "February" 2,
    "March" 3
  • gtgtgt print month
  • 'January' 1, 'February' 2, 'March' 3
  • gtgtgt print month'January'
  • 1
  • gtgtgt print month.keys()
  • 'January', 'February', 'March'
  • gtgtgt monthnames month.keys()
  • gtgtgt print monthnames0
  • January
  • gtgtgt print monthnames1
  • February

20
Python String Operators
  • Concatenation uses

gtgtgt firstname "Derek" gtgtgt lastname
"Harter" gtgtgt fullname firstname " "
lastname gtgtgt print fullname Derek Harter
  • Python strings are a class, use methods to find
    and replace

gtgtgt print fullname.find("ere") 1 gtgtgt print
fullname.find("xyz") -1 gtgtgt print
fullname.replace("ere", "fsf") Dfsfk Harter
21
Python String Operations
  • gtgtgt dir(fullname)
  • 'capitalize', 'center', 'count',
    'decode','encode', 'endswith', 'expandtabs',
    'find', 'index', 'isalnum', 'isalpha', 'isdigit',
    'islower', 'isspace', 'istitle', 'isupper',
    'join', 'ljust', 'lower', 'lstrip', 'replace',
    'rfind', 'rindex', 'rjust', 'rstrip', 'split',
    'splitlines', 'startswith', 'strip', 'swapcase',
    'title', 'translate', 'upper', 'zfill'
  • gtgtgt import string
  • gtgtgt dir(string)
  • 'ascii_letters', 'ascii_lowercase',
    'ascii_uppercase', 'atof', 'atof_error', 'atoi',
    'atoi_error', 'atol', 'atol_error', 'capitalize',
    'capwords', 'center', 'count', 'digits',
    'expandtabs', 'find', 'hexdigits',
    'index','index_error', 'join', 'joinfields',
    'letters', 'ljust', 'lower', 'lowercase',
    'lstrip', 'maketrans', 'octdigits', 'printable',
    'punctuation', 'replace', 'rfind', 'rindex',
    'rjust', 'rstrip', 'split', 'splitfields',
    'strip', 'swapcase', 'translate', 'upper',
    'uppercase', 'whitespace', 'zfill'

22
Python Control Structures
  • Use colon and indentation to show nesting (and no
    terminators)
  • Very different from C/C and shell derived
    languages, takes some getting used to.

python dropped the for (i0 ilt10 i) syntax
a 3 if a lt 0 print less elif a 0
print equal else print greater
a 3 while a gt 0 print a a - 1
for x in range(1,15) print x, print \n
23
Python File I/O
import fileinput for line in fileinput.input()
print -gt, line
file open("hello.py") lines
fd.readlines() file.close() for line in lines
print -gt, line
import fileinput for line in fileinput.input(info
.dat) print -gt, line
24
Python Functions
  • Much cleaner than Perl and shell functions
  • Uses normal parameter type names to pass in
    values to a function

def pounds2dollars(pounds) EXCHANGE_RATE
1.54 return (EXCHANGE_RATE pounds) book
3.0 value of book in British pounds value
pounds2dollars(book) print Value in dollars
, value, \n
25
Python Classes
  • Perl does support OO, but it was tacked onto the
    language
  • Much cleaner in Python

26
class point2d(object) Constructor defines
how to create new objects. def __init__(self)
self.x 0 self.y 0 Get X
coordinate. def getX(self) return self.x
Set X coordinate. def setX(self, newX)
assert newX gt 0 self.x newX Get and
set Y coordinate. def getY(self) return
self.y def setY(self, newY) assert newY gt
0 self.y newY Calculate norm. def
norm(self) return math.sqrt(self.x 2
self.y 2)
Write a Comment
User Comments (0)
About PowerShow.com