Вы находитесь на странице: 1из 21

Your UNIX: The Ultimate Guide

second edition

perl Perl is UNIXs 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 Offices 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

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

Your UNIX: The Ultimate Guide


second edition

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) :`

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

Your UNIX: The Ultimate Guide


second edition

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.

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

Your UNIX: The Ultimate Guide


second edition

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

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

Your UNIX: The Ultimate Guide


second edition

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
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

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
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

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

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

Your UNIX: The Ultimate Guide


second edition

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

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

Your UNIX: The Ultimate Guide


second edition

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

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

Your UNIX: The Ultimate Guide


second edition

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
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

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 *

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

Your UNIX: The Ultimate Guide


second edition

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 conditional if ($x>1) { print(a); } else { print(b); }
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

UNIX The Master Manipulator


The standard Conditional and Loops The if conditional if ($x > 5) { print(a); } elseif ($x > 3) { print(b); } else { print(c); }
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

UNIX The Master Manipulator


The standard Conditional and Loops The if conditional printf() if $x > 3

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

Your UNIX: The Ultimate Guide


second edition

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;
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

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/group perl 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
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

UNIX The Master Manipulator


Reading files from Command Line Arguments while (<>) can be used to read multiple files perl e print while (<>) foo1 foo2 foo3 perl also supports the n option which implies the above loop perl ne print /etc/group perl ne print foo1 foo2 foo3 perl ne print if /wood\b/ emp.lst \b is used to match on a word boundary to eliminate woodhouse and woodcook from the output
Das
2006 The McGraw-Hill Companies, Inc. All rights reserved.

Your UNIX: The Ultimate Guide


second edition

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

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

Your UNIX: The Ultimate Guide


second edition

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

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

Your UNIX: The Ultimate Guide


second edition

UNIX The Master Manipulator


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

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

Your UNIX: The Ultimate Guide


second edition

UNIX The Master Manipulator


List and Arrays A list is a set of data An array makes the list available in a variable

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

Вам также может понравиться