50
1 ITEC 400 Perl and Man Pages George Vaughan Franklin University

ITEC 400 Perl and Man Pages

  • Upload
    jacoba

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

ITEC 400 Perl and Man Pages. George Vaughan Franklin University. Topics. What is Perl? Types Storage Concepts File Interaction Flow Control Pattern Matching Man Pages. What is Perl?. Perl – Practical Extraction and Report Language Created by Larry Wall Evolving Language - PowerPoint PPT Presentation

Citation preview

Page 1: ITEC 400 Perl and Man Pages

1

ITEC 400Perl and Man Pages

George Vaughan

Franklin University

Page 2: ITEC 400 Perl and Man Pages

2

Topics

• What is Perl?

• Types

• Storage Concepts

• File Interaction

• Flow Control

• Pattern Matching

• Man Pages

Page 3: ITEC 400 Perl and Man Pages

3

What is Perl?

• Perl – Practical Extraction and Report Language

• Created by Larry Wall

• Evolving Language

• Current version is Version 5

• Type: perl –v to find the version in your environment

Page 4: ITEC 400 Perl and Man Pages

4

Quick Notes on Syntax

• Quotes behave like those in shell scripts:– Double Quotes: treat contents as a single

value (allowing for variable interpolation).– Single Quotes: treat contents literally.– Backwards Quotes: execute contents

• Backslash interpolation: can be used to encode special characters – (carriage return = \n).

Page 5: ITEC 400 Perl and Man Pages

5

Types

• Perl does not require you to declare variables or variable type.

• Perl will interpret values such as string, integer, float, character, command, etc. based on context.

• Variables not yet defined have values of either “” or zero.

Page 6: ITEC 400 Perl and Man Pages

6

Storage Concepts

• At a simplified level, Perl views variables of being one of two types:

• Singular – variables that hold a single value (scalar). Example: a number or string.

• Plural – variables that represent multiple values (arrays, hashes). Example: an array of numbers or array of strings.

Page 7: ITEC 400 Perl and Man Pages

7

Singular (Scalar) Variables

• Singular (scalar) variables are prefixed with a dollar ($) symbol:$myVar = “A String”;

print $myVar, “\n”;

• Notice that the Perl variable name is prefixed with “$” during assignment in addition to reference (unlike shell).

Page 8: ITEC 400 Perl and Man Pages

8

A Note On Examples• Source for all examples in this and future lectures are on codd (einstein) at:

/export/home/vaughang/public_html/itec400/examples• and on the web at:

– http://cs.franklin.edu/~vaughang/itec400/examples/• All shell examples are written in Korn Shell (ksh) although they should be

quite compatible with Bash.• In all examples, the ‘greater-than’ symbol, ‘>’ will be used to indicate the

shell prompt. So, if we execute the date command,>dateThu Sep 2 20:43:58 EDT 2004

we only type ‘date’ and not ‘>date’.• All scripts should be able to execute on codd (einstein) which is a Solaris

machine, or any Linux distribution that supports Korn shell, located at /bin/ksh.

Page 9: ITEC 400 Perl and Man Pages

9

Example ex03100001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03100005 #0006 # A simple Perl script that demonstrates0007 # the use of singular (scalar) variables0008 ###########0009 $time = `date`; 0010 $text = "Hello, the time is $time\n";0011 print $text;

• Line 1: note to shell that this is a perl script Use path specified by “which perl”.

• Line 9: capture output of date command in scalar variable $time.

• Line 11: print contents of $text to stdout.

• Notice that that each statement is terminated with a semicolon.

• OUTPUT:>ex00310Hello, the time is Sun Sep 19 12:33:19

EDT 2004

Page 10: ITEC 400 Perl and Man Pages

10

How To Execute A Perl Script

• There are several ways:– Type: perl ex0310– Adding “#!/usr/bin/perl -w” to top of script

and setting permissions for execution allows us to simply type: ex0310

– The perl ‘-w’ option will produce warning messages (handy for debugging)…

Page 11: ITEC 400 Perl and Man Pages

11

Plural Variables

• 2 Types: arrays and hashes

• Arrays – useful when using numeric position to retrieve information (example: room number).

• hashes – useful when using a key to retrieve information from a list (example: license plate number)

Page 12: ITEC 400 Perl and Man Pages

12

Arrays

• An ordered list of numbers, strings, and/or references to arrays or hashes.

• Array variables are prefixed with an ‘at’ symbol (@).• Array length is not declared and arrays can grow

dynamically at run time.• $length_of_myArray = scalar(@myArray);• In the next example, you will see how to:

– assign a list to an array– assign an array to a list– assign a value to an element– use an array as a stack

Page 13: ITEC 400 Perl and Man Pages

13

Example ex03200001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03200005 #0006 # A simple Perl script that demonstrates0007 # the use of array (plural) variables0008 ###########00090010 @days = ("mon", "tue", "wed", "thu");0011 ($day1, $day2, $day3) = @days;0012 print "The third day = $day3\n";0013 $days[4] = "fri";0014 print "The fifth day = $days[4]\n";0015 push @days, "sat";0016 push @days, "sun";0017 print "The sixth day = $days[5]\n";0018 print "The seventh day = ",0019 pop @days, "\n";

• Line 10: assign a list of literals to an array.

• Line 11: assign first 3 elements of array to list of singular variables.

• Line 13: assign a value to an array element (note: element is scalar).

• Array indices start at zero.• Lines 15 and 16: Treat array as

stack and push 2 values on stack.• Line 17: print array element• Liine 18 and 19: pop stack and

print its value.• OUTPUT:The third day = wedThe fifth day = friThe sixth day = satThe seventh day = sun

Page 14: ITEC 400 Perl and Man Pages

14

Hashes

• Internally implemented as a hash table.

• Cannot be treated like a stack.

• Collection of “key-value” pairs. – When assigning a list to a hash, the list must

also be a list of “key-value” pairs.

• Hash variables are prefixed with the percent sign (%).

Page 15: ITEC 400 Perl and Man Pages

15

Hashes

• Lists can be assigned to hashes using 2 different forms of syntax:%myHash = (key1, val1, key2, val2, ….);

%myHash = (

key1 => val1,

key2 => val2,

);

Page 16: ITEC 400 Perl and Man Pages

16

Example ex03300001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03300005 #0006 # A simple Perl script that demonstrates0007 # the use of hash (plural) variables0008 ###########00090010 %states_1 = ( "oh", "Ohio", "fl", "Florida",0011 "de", "Delaware" );0012 print "I live in $states_1{oh}\n";00130014 %states_2 = (0015 "oh" => "Ohio",0016 "fl" => "Florida",0017 "de" => "Delaware"0018 );0019 print "I was born in $states_2{de}\n";

• Line 10: assign key-value pair list to hash.

• Line 14: assign key-value pair list to hash using an alternative syntax format.

• OUPUT:

I live in Ohio

I was born in Delaware

Page 17: ITEC 400 Perl and Man Pages

17

Array and Hash Slices

• It is possible to grab slices (subsets) of arrays and hashes.

• The slice itself is always an array.• Arrays:

@myArray[n..m] #n and m is index range@myArray[n, m, o] #n, m, and o are indices

• Hashes:@myHash{‘n’, ‘m’} #n and m are keys

# The @ indicates that# the result is an array.

Page 18: ITEC 400 Perl and Man Pages

18

Example ex03400001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03400005 # array and hash slices0006 ###########00070008 @days = ("mon", "tue", "wed", "thu",0009 "fri", "sat", "sun");0010 @weekdays = @days[0..4];0011 print "weekdays = @weekdays\n";0012 @tdays = @days[1,3];0013 print "tdays = @tdays\n";0014 %states_1 = ( "ca", "California", "fl",0015 "Florida", "de", "Delaware" );0016 @eastStates = @states_1{'fl', 'de'};0017 print "eastStates = @eastStates\n";

• Line 8: create array “days”• Line 10: grab slice of array “days” (i.e.

elements 0 through 4).• Line 12: grab slice of array “days” (i.e.

elements 1 and 3).• Line 14: create hash “states_1”• Line 16: grab slice of hash (entries for

Florida and Delaware)• OUTPUT:$ex0340weekdays = mon tue wed thu fritdays = tue thueastStates = Florida Delaware

Page 19: ITEC 400 Perl and Man Pages

19

File Interaction

• Opening a file for Reading

• Reading from a file

• Opening a file for Writing

• Writing to a file

Page 20: ITEC 400 Perl and Man Pages

20

File Handles

• A file handle is nothing more than a program defined name for a file.

• The file handle name does not have to match the file name.

• The file handle is bound to a file or pipe with the open statement.

Page 21: ITEC 400 Perl and Man Pages

21

Opening Files and Pipes

• Read from an existing file:– open(MY_FILE, “my_file”), open(MY_FILE, “<my_file”)

• Create a file and write to it:– open(MY_FILE, “>my_file”)

• Append to and existing file:– open(MY_FILE, “>>my_file”)

• Set up an output filter:– open(MY_FILE, “| command”)

• Set up an input filter:– open(MY_FILE, “command |”)

• Special File Handles: STDIN, STDOUT and STDERR

Page 22: ITEC 400 Perl and Man Pages

22

Closing a File or Pipe

• close FILE_HANDLE

• A file will be closed automatically if its file handle is used in another open statement.

• All files will be closed upon completion of the script.

Page 23: ITEC 400 Perl and Man Pages

23

Reading from a File or Pipe

• The angle brackets are used for reading a line of input.• $myVar = <MY_FILE> will read one line of text from the

file referenced by MY_FILE.• $myVar = <STDIN> will read a line from standard in

(usually the keyboard).• $myVar = <> will read a line from one or more files on

the command line (or STDIN if no files were specified).• Reading a line from a file or STDIN will also read the

new-line character (/n). Use “chomp()” to remove new-line character.

Page 24: ITEC 400 Perl and Man Pages

24

Example ex03500001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03500005 #0006 # A simple Perl script that demonstrates0007 # the use of working with files0008 ###########00090010 open(PASSWD, "/etc/passwd") or die

"Can't open passwd: $!\n";0011 $total = 0;0012 while ($line = <PASSWD>) {0013 @fields = split(":", $line);0014 ($uid, $name) = ($fields[0], $fields[4]);0015 print "$uid, $name\n";0016 ++$total;0017 }0018 print "total = $total\n";

• Line 10: open file• Line 12: For each loop, assign the

next line of input to $line• Line 13: Break up input along the

field separator, ‘:’, and assign each piece to an array element.

• Line 14: multiple assignments• OUPUT (subset):vaughang, George Vaughanswartwod, Don Swartwoutpandarir, Raghavender R. Pandariettefagm, Mohamad Ettefaghfleigd, David Fleigforsheyr, Ralph Forsheytotal = 255

Page 25: ITEC 400 Perl and Man Pages

25

String Operators

• Most of the familiar operators are described in the book.

• String Concatenation Operator “.”$a = $b . $c;

$a .= $b;

• String Multiplication Operator “x”$a = “a” x 3; #$a = “aaa”

Page 26: ITEC 400 Perl and Man Pages

26

Example ex03550001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03550005 # Create histogram of processes per user0006 ###########0007 open(PROCESSES, "ps -ef | sed '/UID/d' |")0008 or die "Can't open processes: $!\n";0009 $marker = "*";0010 while ($line = <PROCESSES>) {0011 @fields = split(" ", $line);0012 $uid = $fields[0];0013 $processes{$uid}++;0014 }00150016 foreach $uid (sort keys %processes) {0017 printf ("%8s: %4d ", $uid,

$processes{$uid});0018 print $marker x $processes{$uid}, "\n";0019 }

• Line 7: Open pipe using output of ‘ps’• Lines 10-14: count number of

processes per user• Lines 16-19: For each user on the

system, print name, number of processes and chart.

• Line 17: Notice use of printf function to format output.

• Line 18: string multiplication

• OUTPUT (Subset):vaughang: 4 **** velez04: 1 *wachir01: 1 * wangt: 1 * yang05: 2 ** yeh04: 2 ** yehpo: 2 **

Page 27: ITEC 400 Perl and Man Pages

27

Math Operators

• Very similar to C.

• Book provides good overview

Page 28: ITEC 400 Perl and Man Pages

28

Comparison Operators

Comparison Numeric String

Equal == eq

Not Equal != ne

Less Than < lt

Greater Than > gt

Less Than or Equal <= le

Greater Than or Equal >= ge

Comparison <==> cmp

Page 29: ITEC 400 Perl and Man Pages

29

Conditional File Operators

operator Comments

-e $myFile Does $myFile exist?

-r $myFile Is $myFile readable by me?

-w $myFile Is $myFile writable by me?

-d $myFile Is $myFile a directory?

-f $myFile Is $myFile a regular file?

-T $myFile Is $myFile a text file?

Page 30: ITEC 400 Perl and Man Pages

30

Flow Control

• “if” and “if, else”

• “unless”

• “while”

• “for”

• for each”

Page 31: ITEC 400 Perl and Man Pages

31

“if” and “if, else”

• “if”if (conditional_expr) {

statement_list

}

• “if, else”if (conditional_expr) {

statement_list

}

else {statement_list

}

• Note: curly braces are mandatory

Page 32: ITEC 400 Perl and Man Pages

32

“elsif” and “unless”

• “elsif”if (conditional_expr) {

statement_list

}

elsif (conditional_expr) {statement_list

}

• “unless”unless (conditional_expr) {

statement_list

}

• “elsif” statements can be daisy-chained.

• an “elsif” statement can be followed by an “else” statement.

• “unless” is like saying:if something is false,

then execute block.

Page 33: ITEC 400 Perl and Man Pages

33

Example: ex03600001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03600005 # Demo of if, elsif, unless0006 ###########0007 unless (scalar(@ARGV)==2) {0008 print "usage: ex0360 v1 v2",0009 "\n";0010 exit 1;0011 }0012 if ($ARGV[0] > $ARGV[1]) {0013 print "number $ARGV[0] is greater ";0014 print "than $ARGV[1]\n";0015 }0016 else {0017 print "number $ARGV[0] is less than";0018 print " or equal to $ARGV[1]\n";0019 }0020 exit 0;

• Line 7: execute block if condition is false.

• Line 7: test if there are exactly 2 command line arguments.

• Line 10: exit with return status of 1.• Line 12: Numerical comparison of

command line argument 1 and 2.• Output 1:

>ex0360 3 2number 3 is greater than 2

• Output 2:>ex0360 2usage: ex0360 v1 v2

Page 34: ITEC 400 Perl and Man Pages

34

“while” and “do while” Loops

• “while”while (conditional_expr) {

statement_list

}

• “do while”do {

statement_list

} while (conditional_expr) ;

• See ex0350 for example of “while”

• “while” loop will execute zero or more times.

• “do while” loop will execute one or more times.

Page 35: ITEC 400 Perl and Man Pages

35

“until” and “do until” Loops

• “until”until (conditional_expr) {

statement_list

}

• “do until”do {

statement_list

} until (conditional_expr) ;

• “until” loops are like “while” loops except we loop as long as the conditional expression is false.

• “until” loop will execute zero or more times.

• “do until” loop will execute one or more times.

Page 36: ITEC 400 Perl and Man Pages

36

“for” Loops

for (a; b; c) {

statement_list

}where:• a = initial conditions• b= conditional expression• c = iterative action

• Same format as C, C++ and Java.

Page 37: ITEC 400 Perl and Man Pages

37

Example: ex03650001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03650005 # Demo of 'for' loop0006 ###########00070008 #print out the numbers 3 to 60009 for ($i=3; $i <=6; ++$i) {0010 print $i, "\n";0011 }00120013 print "\n";00140015 #print the even number0016 #between 2 and 100017 for ($i=2; $i<10; $i=$i+2) {0018 print $i, "\n";0019 }

• Lines 9 – 11: for loop print number from 3 to 6

• Lines 17-19: for loop prints even numbers between 2 and 10

• OUTPUT:>ex03653456

2468

Page 38: ITEC 400 Perl and Man Pages

38

“for each” Loops

foreach $var (list) {

statement_list

}

• Loop for each element in list.

• For each iteration, set $var to be an alias to the next element in the list.

• See ex0355

Page 39: ITEC 400 Perl and Man Pages

39

Pattern Matching

• “//” is the pattern matching operator - used to match regular expressions.

• /good/ - match on literal string “good”• if ($myVar =~ /good/)

– true if $myVar contains “good” (e.g. “good dog”, “goody”)

– “=~“ is the pattern binding operator

• if (/good/)– true if default string ($_) contains “good”.

Page 40: ITEC 400 Perl and Man Pages

40

Regular Expressions

• Previous slide showed matching on literals.• We can also match on regular expressions:

– /a[a-g]/ - match on a string that contains a substring that starts with “a” and is followed by a letter between “a” and “g”, inclusive. Example: “ab”

– /a[a-g]*z/ - match on a string that contains a substring that starts with “a”, followed by zero or more characters from “a” to “g” inclusive and ends with “z”. Examples: “aaz”, “abz”, “yabababz”, “gaz”

– /a[a-g]+z/ - match on a string that contains a substring that starts with “a”, followed by one or more characters from “a” to “g” inclusive and ends with “z”. Examples: “aaz”, “abz”, “yabababz”, NOT “gaz”

Page 41: ITEC 400 Perl and Man Pages

41

Example ex03700001 #!/usr/bin/perl -w00020003 ###########0004 # File: ex03700005 # Demo of if, elsif, unless0006 ###########0007 $_ = $ARGV[0];0008 if ($ARGV[0] =~ /a[a-g]/){0009 print "$ARGV[0] is in the ";0010 print "language \"a[a-g]\"\n";0011 }0012 if (/a[a-g]*z/){0013 print "$ARGV[0] is in the ";0014 print "language \"\[a-g]*z\"\n";0015 }0016 if (/a[a-g]+z/){0017 print "$ARGV[0] is in the ";0018 print "language \"a[a-g]+z\"\n";0019 }

• Line 7: assign the first command line argument to the default string.

• Examples:

$ex0370 abab is in the language "a[a-g]“

$ex0370 azaz is in the language "[a-g]*z“

$ex0370 abzabz is in the language "a[a-g]"abz is in the language "[a-g]*z"abz is in the language "a[a-g]+z"

Page 42: ITEC 400 Perl and Man Pages

42

Man Pages

• Man pages are online documents that describe the use of commands, system calls and other functions.

• All Unix systems have a set of man pages for Unix commands, system calls, functions etc.

• Applications may also come with their own set of man pages which are usually maintained in an area separate from system man pages.

Page 43: ITEC 400 Perl and Man Pages

43

Man Page Sections

• Man Pages are broken up into different categories.

• The categories are identified by section number (Linux and Solaris):

Section Contents

1 User Commands

1m Admin Commands

2 System Calls

3 Functions

4 Configuration Files and formats

5 Miscellaneous

6 (1) Games, Demos

7 (9) Special Files and Hardware

8 Maintenance Commands

Page 44: ITEC 400 Perl and Man Pages

44

Man Page Usage

• To use, simply type: man command• Example: man sleep• Sometimes a name will appear in more than one

section (name maybe command and function).• To specify a specific section to search, type:

man –s2 time (will show system call version instead of command version)

• Solaris uses lower case ‘s’ and Linux uses capital ‘S’ in –s option

Page 45: ITEC 400 Perl and Man Pages

45

More Than One Set

• It is quite common that applications have their own sets of man pages.

• If many applications are installed, users may be interested in having access to multiple sets of man pages.

• The different sets of man pages that will be searched and the order they are searched are stored in the environment variable: $MANPATH

Page 46: ITEC 400 Perl and Man Pages

46

More Than One Set

• A user may add a new set by appending the path of the man pages to $MANPATH.

• Example:MANPATH=$MANPATH:$LOGNAME/itec400/man

export MANPATH

Page 47: ITEC 400 Perl and Man Pages

47

How Do You Create Your Own Man Pages

• First, you need to create a directory to hold the man pages:mkdir ~/itec400/man

• Next, you need to create subdirectories for those sections that might have man pages.

• Assume you will only have commands:mkdir ~/itec400/man/man1

Page 48: ITEC 400 Perl and Man Pages

48

How Do You Create Your Own Man Pages

• Next you need to create a file that describes the command.

• Assume the command name is ‘mycommand’• the man file name for this command would be:

‘mycommand.1’• So, ‘mycommand.1’ would be stored in

~/itec400/man/man1• Finally, we need to change $MANPATH:

MANPATH=$MANPATH:~/itec400/manexport MANPATH

• Now we can type: man mycommand

Page 49: ITEC 400 Perl and Man Pages

49

How Do You Create Your Own Man Pages

• Man page files are text files, however, they use a special formatting language: nroff

• nroff has its man page

• Sometimes it is easiest to copy and modify the source file of an existing man page

• For example, the source file for gcc is:/usr/local/man/man1/gcc.1

Page 50: ITEC 400 Perl and Man Pages

50

References

• “Programming Perl”, Larry Wall, Tom Christiansen and Jon Orwant, 2000

• “Essential System Administration”, by Aeleen Frisch, 2002