Perl Programming ch 11 1 Introduction Perl Practical

  • Slides: 12
Download presentation
Perl Programming (ch 11) 1

Perl Programming (ch 11) 1

Introduction • Perl – Practical Extraction and Report Language – Larry Wall, 1987 –

Introduction • Perl – Practical Extraction and Report Language – Larry Wall, 1987 – Scripting Language • Scheme, Python, Tcl, Ruby, … • Interpreted 2

Get Started • Print out – – ~it 244/scripts/simple. pl perl simple. pl chmod

Get Started • Print out – – ~it 244/scripts/simple. pl perl simple. pl chmod a+x simple. pl. /simple. pl –. /simple 2. pl – perl –e ‘print “hin”’ 3

Get Started • Basic syntax – Semicolon(; ) at the end of each statement

Get Started • Basic syntax – Semicolon(; ) at the end of each statement – Double/single quote • ~it 244/scripts/quote. pl – Special characters (Table 11 -1) • n, t, 4

Control Structure • if, if. . else, if. . else if (condition) {…} –

Control Structure • if, if. . else, if. . else if (condition) {…} – Comparison (numbers) • ==, !=, <, >, … • ~it 244/scripts/age. pl • $var=<> 5

Control Structure • for/foreach $item (“mo”, “larry”, “curly”){ say “$item says hello. ”; }

Control Structure • for/foreach $item (“mo”, “larry”, “curly”){ say “$item says hello. ”; } foreach (“mo”, “larry”, “curly”){ say “$_ says hello. ”; } ~it 244/scripts/foreach. pl 6

Control Structure • while/until while (condition) {…} ~it 244/scripts/while 1. pl $. /while 1.

Control Structure • while/until while (condition) {…} ~it 244/scripts/while 1. pl $. /while 1. pl – $. : number of input lines, $_: default operant – ~it 244/scripts/while 2. pl • last and next – break and continue 7

File Operation • File handle (*file descriptor) open (file-handle, [‘mode’], “file-ref”) *exec file-descriptor <&

File Operation • File handle (*file descriptor) open (file-handle, [‘mode’], “file-ref”) *exec file-descriptor <& filename – File-handle: a variable, a number – Mode: <, >, >>, ‘read’ by default if not specified – File-ref: file name 8

File Operations • Read from files open (file-handle, [‘mode’], “file-ref”) $line=<file-handle> $line=<> ~it 244/scripts/readfiles.

File Operations • Read from files open (file-handle, [‘mode’], “file-ref”) $line=<file-handle> $line=<> ~it 244/scripts/readfiles. pl ~it 244/scripts/readfiles 2. pl 9

File Operations • Write to files open (file-handle, [‘mode’], “file-ref”) print file-handle “text” *echo

File Operations • Write to files open (file-handle, [‘mode’], “file-ref”) print file-handle “text” *echo “text” >&file-descriptor ~it 244/scripts/writefiles. pl 10

Command-line Arguments • Variables – Scalar (singular) • $name=“Sam”; – Array (plural) • @array=(1,

Command-line Arguments • Variables – Scalar (singular) • $name=“Sam”; – Array (plural) • @array=(1, 2, “Sam”, “Max”); – $array[1], @array[1, 2], @array[1. . 3] » ~it 244/scripts/arrays. pl – @array, @#array » ~it 244/scripts/arrays 2. pl 11

Command-line Arguments • Command-line arguments – @ARGV • ~it 244/scripts/args. pl • . /args.

Command-line Arguments • Command-line arguments – @ARGV • ~it 244/scripts/args. pl • . /args. pl a b c – $ARGV (<>) • ~it 244/scripts/file 3. pl • . /file 3. pl file 1 file 2 file 3 12