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

Post on 17-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Perl

COEN 351

Thomas Schwarz, S.J. 2006

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.

Perl

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

Installing and Running Perl

COEN 351

Thomas Schwarz, S.J. 2006

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.

Perl Fundamentals

COEN 351

Thomas Schwarz, S.J. 2006

Perl

Simple “hello world” application.

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

Perl: Scalar Variables

Perl: Scalar Variables

Interpolation Perl will “interpolate” scalar values in

strings.

Perl: Scalar Variables

Variables: Undefined variables have a standard

value. 0 for numbers. Empty string for strings.

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”;

Perl: Array Variables

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

Perl: Input / Output

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.

Perl Input / Output

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

chomp(@lines = <STDIN>;

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 %.

Perl: Hashes

top related