



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Professor: Lyle; Class: Scripting Languages; Subject: CSC Computer Science; University: Murray State University; Term: Unknown 1989;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Chapter 1: Introduction Excellent overview of Perl and source of Perl resources online. See the simple “Hello World” program: print' is used to produce output and # indicates a comment. In Perl version 5.10, you can use 'say' instead of 'print'; must also include the line 'use 5.010;' at the beginning of the program.
Chapter 2: Scalar Data Numbers – all numbers are stored as doubles; arithmetic operators are +, -, *, /, %, **, for addition, subtraction, multiplication, division, modulus, and exponentiation respectively.
Strings – sequences of characters enclosed in either single quotes or double quotes; backslash escapes (e.g. \n or \t, see Table 2.1, p. 23-24 are interpreted inside double quotes but are treated as standard characters inside single quotes. The string concatenation operator is the period '.'; Perl automatically converts numbers to strings and vice versa as required.
Scalar variables begin with a $ sign, followed by an underscore or letter and then possibly more letters, digits, and underscores.
Assignment is accomplished with = operator; there are also the +=, -=, *= , /=, %=, and .= operators.
Variable interpolation in strings -- Double quoted strings that contain a variable name will have the variable replaced by its value.
See Table 2-2, p. 31-32 for operator precedence and associativity.
The comparison operators are ==, !=, <, <=, >=, and > for numeric values and eq, ne, lt, le, gt, and ge for strings.
The if statement – if ( condition ) { ... statements ...}
The line input operator --
The while loop – while ( condition ) { ... statements ... }
Scalar variables have the special value undef before they are assigned a value. This will be used as 0 or an empty string in expressions. The defined() function can test a variable for the undef value.
Chapter 3: Lists and Arrays A list is an ordered collection of scalars. An array is a variable that contains a list. The individual values can be strings, numbers, or undef values, and need not all be of the same type. Subscripts are integers
and always begin at 0. Arrays are automatically extended to accommodate previously non-existent elements. The last subscript of an array named rocks is given by $#rocks. You can remove the tail or extend an array by modifying the value of this variable. You may also use negative subscripts to refer to elements from the end of the array.
List literals contain 0 or more values in parentheses separated by commas, but also may contain references to other variables or ranges of values: (1, 2..5, 7, “larry”) is the same as (1, 2, 3, 4, 5, 7, “larry”). The qw shortcut stands for “quoted words” or “quoted by whitespace”, so (“Larry”, “Moe”, Curly”) is the same as qw / Larry Moe Curly/. The delimiter need not be a '/', but could be almost any punctuation character.
List literals may appear on the right hand side of an assignment and may be used to assign to an array by using the '@' before the array name: @stooges = qw / Larry Moe Curly/
The push and pop operators allow you to add and remove elements at the end of an array, so $fred = pop( @array ) removes the last element from array (permanently) and assigns it to $fred, while push( @array, $a) would put $a as the last element of array.
The shift and unshift operators work on the beginning of an array in the same way that push and pop work on the end of the array.
Entire arrays will be interpolated into quoted strings using @array: if @array = qw {one two three}, then “zero @array end” will be the same as “zero one two three end”.
The foreach loop gives a convenient method to iterate over all the elements in a list or an array.
The reverse operator takes a list (or an array that contains a list) and returns the list in reverse order. The sort operator likewise returns a list in sorted (ASCIIbetical) order. This will probably not give the desired results if the list contains numbers.
VERY important section on Scalar and List context. The name of an array returns a list in a list context and the number of elements in a scalar context. Be sure to read and understand this section and all of the examples in it.
In a scalar context,
Chapter 4: Subroutines Subroutines are defined by the word sub, followed by the name of the subroutine, then the block of code in curly braces which makes up the body of the subroutine. A subroutine is invoked by preceding its name with the '&' symbol. The value of the last expression in the subroutine is the value that is returned. The parameters to a subroutine are stored in the (local) array @_, which can be tested to see how many actual parameters were sent. By default, all Perl variables are global in scope. They can be declared local to a subroutine using the 'my' operator. The return operator can specify a value to be returned, instead of the last expression the subroutine. A subroutine may be invoked without the preceding '& if the compiler can determine that it really is a subroutine call (the definition came earlier or the parameters are
while(
You can use the select statement to change the default output filehandle. Setting the special variable $| = 1; forces the output buffer to be flushed after each output operation.
The say keyword is similar to print, but it adds a newline.
Chapter 6: Hashes A hash is similar to an array, except that the subscripts, called keys, are strings instead of integers, but must be unique. Individual values in a hash are accessed by enclosing the key value in curly braces, similar to square brackets and integer subscripts for arrays. To refer to the entire hash, use the percent sign ('%') as a prefix. When assigning a list to a hash, the values in the list occur in pairs as “key”, “value”, “key”, “value”, etc. The assignment can also use the “big arrow” technique whereby the “key” and value are separated by '=>', instead of a comma. Thus, the list is ( key => value, key => value, ... ).
The keys function applied to a hash returns the list of key values, in no particular order. The values function returns the list of values in the same order as the keys function returned. In a scalar context, these functions return the number of entries in the hash. You can iterate over all pairs in a hash using the following loop: while( ($key, $value) = each %hash ) {... body of loop ...}
The exists function allows you to determine if a particular key value is defined in the hash. The delete function will delete the key and its value from the hash.
There is a special hash, %ENV, that includes the environment in which your program is executed.
Chapter 7: In the World of Regular Expressions Regular expressions specify patterns to be used for matching with strings: These can be specific characters enclosed in delimiters (usually /'s), often combined with metacharacters and quantifiers:
dot (.) matches any single character, except \n backslash () is used to 'escape' other metacharacters star (*) means to repeat the previous item zero or more times plus (+) means to repeat the previous item one or more times question mark (?) means the preceding item is optional parentheses are used to group items vertical bar (|) means 'or' when it appears between two items
Character class – sequence of characters enclosed in square brackets [], matches any one of these characters; can specify a range: [a-zA-Z] matches any letter. The caret, ^, negates the class.
Shortcuts: [0-9] abbreviated as \d; \w (a “word” character) stands for [a-zA-Z0-9_], \s ( a “space” character stands for [\f\t\n\r ]. These are negated with the ^, as [^\d] matches a non-digit character. The negations have shortcuts: \D, \W, and \S respectively.
See discussion of parentheses and back references to refer to which parts of the string actually matched the pattern.
Chapter 8: Matching with Regular Expressions Matches with m//: matches with / / don't need the 'm'; any other delimiter does need the 'm'.
Option Modifiers: /i means case insensitive match; /s allows newlines to be matched; /x allows you to add whitespace to the pattern to make it more readable; multiple modifiers can be combined; others will be introduced later.
Anchors force a pattern to match at a particular location within the string. The caret ^ anchors at the beginning of the string and $ anchors at the end; \b matches at either end of a word (a \w kind of word). \B is a non-word boundary, i.e. matches wherever \b would not.
Binding Operator, =~: instead of using $_, =~ uses the string on the left for matching to the pattern on the right; looks like an assignment, but isn't.
Interpolating into patterns: A regular expression is double quote interpolated as if it were a double- quoted string.
Match Variables: Memory Parentheses not only group items, but also tell the regular expression engine to remember what was in the substring that matched what was in the pattern. Thus $1 includes the part of the string that matched the pattern in the first pair of parentheses, $2 contains the matching part from the second pair, etc. There are three match variables that you always get: $, $&, and $'. The part of the string that matched a pattern is stored in $&; $
contains what preceded the match and $' contains whatever is after the match.
Noncapturing Parenteses: You can follow a left parenthesis with ?: to indicate that it is not a cpaturing parenthesis. Perl 5.10 allows us to provide names for the captures.
Quantifiers: means to repeat the preceding item a certain number of times, so /a{5,15}/ matches from 5 to 15 a's, /(fred){3,}/ matches 3 or more repetitions of fred and /\w{8}/ matches exactly 8 word characters. Thus * is the same as {0,}, the + is the same as {1,}, and? is the same as {0,1}.
Precedence of meta-characters: 1. Parentheses used for grouping and and memory. 2. Quantifiers (*, +, ?, and numeric ones with curly braces. 3. Anchors (^, $, \b, \B) and sequences of characters. 4. Vertical bar (|)
The program on page 132 can be used to test potential patterns to verify that they match input strings as expected.
continue in C++); redo (go back to the top of the loop block); Labeled blocks (attach labels to loop blocks to identify them, usually all caps; used to refer to that loop with last, next, etc.)
Ternary operator, ?: expression? if_true_expr : if_false_expr
Logical operators: && means AND; || means OR; if the left side of one of these determines the entire value of the expression, then the right side is not evaluated, i.e., it is short circuited.
Perl 5.10 provides the defined-or operator //