Perl COEN 351 Thomas Schwarz S J 2006
- Slides: 18
Perl COEN 351 Thomas Schwarz, S. J. 2006
Perl n Scripting Language n n Developed by Larry Wall 1987 to speed up system administration tasks. Design principles n n “Easy things should be easy. ” “Hard things should be possible. ” “Many ways to do the same thing. ” Desired similarity to natural languages: n Context important.
Perl n Due to its popularity, there are outstanding resources on the web. n n www. perl. com www. perl. org
Installing and Running Perl COEN 351 Thomas Schwarz, S. J. 2006
Perl n n Perl is quasi-native to Unix systems. Activestate (www. activestate. com) distributes free windows version. n n n After installation, add Perl bin directory to your path. Associate. pl with Perl. exe in Explorer Test perl: n “perl –v” in command prompt.
Perl Fundamentals COEN 351 Thomas Schwarz, S. J. 2006
Perl n Simple “hello world” application.
Perl: Scalar Variables n Variables: n n Untyped, but characterized by first character: Scalar: Numbers or strings. Start out with $: $number, $string, … n n n 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 nondecimal bases: 0377, 0 xff, 0 b 1111111 (all 255 10) Normal operations between numbers “. ” is the string concatenation operator, “x” the string multiplier operator
Perl: Scalar Variables
Perl: Scalar Variables n Interpolation n Perl will “interpolate” scalar values in strings.
Perl: Scalar Variables n Variables: n Undefined variables have a standard value. n n 0 for numbers. Empty string for strings.
Perl: Array Variables n n n Perl implements (C-style) arrays with the beginning character ‘@’ Arrays are automatically maintained. Individual members can be accessed by using the first character ‘$’ $string[0] = “zero”; $string[1] = “one” $string[5] = “five”; n Creates lists with six elements and three undefined entries.
Perl: Array Variables n 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 n Perl uses file handles, including standard input and output. $line = <STDIN> n n The input comes with a newline character attached. You can remove this with the “chomp” command.
Perl Input / Output n Can obtain an array of input lines until user types Control+D (unix) or Control+Z (windows): chomp(@lines = <STDIN>;
Perl: Hashes n n Hash items contain keys and records. We look up a hash record by key. n n Key is a string Hash table itself starts out with a %.
Perl: Hashes