21
Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. second edition Your UNIX: The Ultimate Guide UNIX – The Master Manipulator • perl • Perl is UNIX’s latest major acquisition, and one of its finest. • Developed by Larry Wall, this Practical Extraction and Report Language is often hailed as the Swiss Army Office’s Knife of the UNIX System. • perl is standard on Linux and also offered on Solaris. • It is free, and executables are available for all UNIX flavors

Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Embed Size (px)

Citation preview

Page 1: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• perl

• Perl is UNIX’s latest major acquisition, and one of its finest.

• Developed by Larry Wall, this Practical Extraction and Report Language is often hailed as the Swiss Army Office’s Knife of the UNIX System.

• perl is standard on Linux and also offered on Solaris.

• It is free, and executables are available for all UNIX flavors

Page 2: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• perl Preliminaries

• A perl script or program runs in a special interpretive mode:• the entire script is compiled internally in memory before it is executed.

• Unlike other interpreted languages like the shell, script errors are generated before execution.

• test whether perl is in your PATH:• perl –e `print(“GNUs Not Unix\n”) :`

Page 3: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• perl Preliminaries

• perl –e can do a lot of useful things from the command line

• perl programs are often big and are much better placed in .pl or .cgi files

• the sample.pl program uses the she-bang line, pointing to the perl command.

• perl uses the # as the comment character exactly like shell.

Page 4: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• perl Preliminaries

• perl mimics awk by supporting the print and printf functions and C by terminating all statements with a semicolon

• perl uses the $ prefix both in the definition ($name = <STDIN>), as well as in evaluation (The temperature, $name ….).

• perl uses <STDIN> to represent standard input.

• the printf function does not use parenthesis

Page 5: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• perl Preliminaries

• If you do not provide the she-bang line (!/usr/bin/perl), then use perl sample.pl to the script

• chomp removes the trailing \n

• perl provides detailed and extensive documentation, which is accesses with the perldoc comand – try perldoc –h

• The newsgroup comp.lang.perl discusses problems related to perl and also posts its FAQ

Page 6: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Variables and Constants

• Variables are considered declared when they are first used.

• perl uses the $ prefix to identify variables both in assignment and evaluation ($x=1; print $x;) The $ prefix is also used by perl with a variable that represents an array element

• Variables and literals have no intrinsic type. perl understands ‘foo’, “foo” and “123” as strings, and 123, 0.0 as numeric

• perl makes the necessary conversions when handling expressions that use a mix of string and numeric data types

Page 7: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Variables and Constants

• for example “123” + 1 is 124; and 1 + “www” is “1www”

• Uninitialized variables are assumed to be null string when treated as a string and zero when treated as a number. Incrementing an uninitialized variable returns 1:• $ perl –e ‘$x++ ; print(“$x\n”);’

• perl is indeed unusual; it can perform computation on any string containing only letters and numbers

Page 8: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Variables and Constants

• perl is indeed unusual; it can perform computation on any string containing only letters and numbers; perl is intelligent enough to guess your intentions:

• $x = “A” ; $x++; $x is B• $y = “P1” ; $y++ ; $y becomes P2• $z = “Q09”; $z becomes Q10

Page 9: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Variables and Constants

• Apart from the standard escape sequence, perl supports \U and \u characters

• $name = “steve jobs”;• result = “\U$name\E”; $result is STEVE JOBS \E terminates action \U• result = “\u$name\E”; $result is Steve jobs• The \L and \l sequences convert string to lowercase in the same way

Page 10: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Operators

• Like all languages, the +, -, *, /, and % operators are used by perl for computation except that perl uses ** for exponentiation (2**10=1024)

• Apart from the comparison and logical operators, perl has two special operators for concatenation and repetition – TBD

• The Comparison and Logical operators• <, <=, ==, >=, >, !=, <==> (returns three values; -1 if left operand is less than the right operand, 0 if equal and 1 otherwise), . (concatenates), x repeats a string

Page 11: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Operators

• if ($name eq “”) – do not use == to compare strings only numbers

• perl also uses logical AND and OR operators && and || • if ($x < 10 && $x > 5)

• perl –e ‘$x=sun ; $y=“.com” ; print ($x . $y . “\n”);’

• $perl –e ‘print “*” x 40;’ will display 40 *

Page 12: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• The standard Conditional and Loops

• Most of the control structures used by shell and C are supported by perl. curly braces?

• The if conditionalif ($x>1) { print(“a”);} else { print(“b”);}

Page 13: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• The standard Conditional and Loops

• The if conditionalif ($x > 5) { print(“a”);} elseif ($x > 3) { print(“b”);}else { print(“c”);}

Page 14: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• The standard Conditional and Loops

• The if conditionalprintf(“”) if $x > 3

Page 15: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• The standard Conditional and Loops

• The while, do-while, and for loops• for ($i = 0; $i < 5; $i++) { printf(“*”); }

• while (1) { printf(“*”); }

• print(“*”) while $x++ < 5;

Page 16: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Reading files from Command Line Arguments

• The diamond, <>, is used for reading lines from a file. For example <STDIN> reads a line from standard input, and <> reads a line from the filename specified as arguments

perl –e ‘print while (<>)’ /etc/groupperl –e ‘print <>’ /etc/group loop is implied

• The first use of <> is interpreted in scalar context which signifies reading one line; and the second is interpreted in list context where it represents all lines

Page 17: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• Reading files from Command Line Arguments

• while (<>) can be used to read multiple filesperl –e ‘print while (<>)’ foo1 foo2 foo3

• perl also supports the –n option which implies the above loop

perl –ne ‘print’ /etc/groupperl –ne ‘print’ foo1 foo2 foo3perl –ne ‘print if /wood\b/’ emp.lst\b is used to match on a word boundary to eliminate woodhouse and woodcook from the output

Page 18: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• String Handling Functions

• chop & chomp• to remove the last character of a string• chomp removes only the newline

• index (str, substr, n) and rindex(str, substr, n)• index returns the first position of the first occurrence of substr in large string str; if n is specified then n characters are skipped• rindex locates the last occurence of the string

Page 19: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• String Handling Functions

• reverse(str)• reverses the characters and returns the reversed string

• substr(str1, offset, length, str2)• if $x is assigned “abcdefg”, substr($x, 4, 3) returns efg$x = “abcdijklm”substr($x, 4, 0) = “efgh”$x is now abcdefghijklm

Page 20: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• String Handling Functions

• substr(str1, offset, length, str2)$y = substr($x, -3, 2); $y is kl

Page 21: Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is

Das© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

second editionYour UNIX: The Ultimate Guide

UNIX – The Master Manipulator

• List and Arrays

• A list is a set of data• An array makes the list available in a variable