18
Perl COEN 351 Thomas Schwarz, S.J. 2006

Perl COEN 351 Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Embed Size (px)

Citation preview

Page 1: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl

COEN 351

Thomas Schwarz, S.J. 2006

Page 2: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl Scripting Language

Developed by Larry Wall 1987 to speed up system administration tasks.

Design principles “Easy things should be easy.” “Hard things should be possible.” “Many ways to do the same thing.” Desired similarity to natural languages:

Context important.

Page 3: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl

Due to its popularity, there are outstanding resources on the web. www.perl.com www.perl.org

Page 4: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Installing and Running Perl

COEN 351

Thomas Schwarz, S.J. 2006

Page 5: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl

Perl is quasi-native to Unix systems. Activestate (www.activestate.com)

distributes free windows version. After installation, add Perl bin directory

to your path. Associate .pl with Perl.exe in Explorer Test perl:

“perl –v” in command prompt.

Page 6: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl Fundamentals

COEN 351

Thomas Schwarz, S.J. 2006

Page 7: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl

Simple “hello world” application.

Page 8: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Scalar Variables Variables:

Untyped, but characterized by first character: Scalar: Numbers or strings. Start out with $:

$number, $string, … All numbers have internally the same format. String literals can be defined with single or double

quotes. There are different rules about special symbols.

Number literals have to follow special formatting for non-decimal bases: 0377, 0xff, 0b1111111 (all 25510)

Normal operations between numbers “.” is the string concatenation operator, “x” the

string multiplier operator

Page 9: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Scalar Variables

Page 10: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Scalar Variables

Interpolation Perl will “interpolate” scalar values in

strings.

Page 11: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Scalar Variables

Variables: Undefined variables have a standard

value. 0 for numbers. Empty string for strings.

Page 12: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Array Variables Perl implements (C-style) arrays with the

beginning character ‘@’ Arrays are automatically maintained. Individual members can be accessed by using

the first character ‘$’

Creates lists with six elements and three undefined entries.

$string[0] = “zero”;$string[1] = “one”$string[5] = “five”;

Page 13: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Array Variables

Use pop, push, shift, unshift to add or remove elements at the end or the beginning of an array.

Page 14: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Input / Output

Page 15: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Input / Output

Perl uses file handles, including standard input and output.$line = <STDIN> The input comes with a newline

character attached. You can remove this with the

“chomp” command.

Page 16: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl Input / Output

Can obtain an array of input lines until user types Control+D (unix) or Control+Z (windows):

chomp(@lines = <STDIN>;

Page 17: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Hashes

Hash items contain keys and records.

We look up a hash record by key. Key is a string

Hash table itself starts out with a %.

Page 18: Perl COEN 351  Thomas Schwarz, S.J. 2006. Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles

Perl: Hashes