\def\revision{1} \documentclass[epsf]{pr} \begin{document} \begin{titlepage} \hbox{ } \end{titlepage} \unnumbered{Contents} \tableofcontents \unnumbered{Conventions} \begin{enum}{2cm} \Xi{|fixed|} denotes text that you enter literally. \Xi{} means variable text, i.e. things you must fill in. \Xi{\dag} means that will default to |$_| if omitted. \Xi{\kwd{word}} is a keyword, i.e. a word with a special meaning. \Xi{\fbox{}} denotes pressing a keyboard key. \Xi{[\ldots]} denotes an optional part. \end{enum} \clearpage \section{Command line options} \begin{enum}{1.4cm} \Xi{|-a|} turns on autosplit mode when used with |-n| or |-p|. Splits to |@F|. \Xi{|-c|} checks syntax but does not execute. It does run |BEGIN| and |END| blocks. \Xi{|-d| [ |:| ]} runs the script under the debugger. Use `|-de 0|' to start the debugger without a script. \Xi{|-D| } sets debugging flags. \Xi{|-e| } may be used to enter a single line of script. Multiple |-e| commands may be given to build up a multi-line script. \Xi{|-F| } specifies a regular expression to split on if |-a| is in effect. \Xi{|-h|} prints the Perl usage summary. Does not execute. \Xi{|-i| } files processed by the |<|\,|>| construct are to be edited in-place. \Xi{|-I| } with |-P|: tells the C preprocessor where to look for include files. The directory is prepended to |@INC|. \Xi{|-l| [ ] } enables automatic line ending processing, e.g. |-l013|. \Xi{|-m| } imports the before executing the script. may be followed by a `|=|' and a comma-separated list of items. \Xi{|-M| } Same as |-m|, but with more trickery. \Xi{|-n|} assumes an input loop around the script. Lines are not printed. \Xi{|-p|} assumes an input loop around the script. Lines are printed. \Xi{|-P|} runs the C preprocessor on the script before compilation by Perl. \Xi{|-s|} interprets `|-xxx|' on the command line as a switch and sets the corresponding variable \$|xxx| in the script. \Xi{|-S|} uses the |PATH| environment variable to search for the script. \Xi{|-T|} turns on \textit{taint} checking. \Xi{|-u|} dumps core after compiling the script. To be used with the \textit{undump} program (where available). \Xi{|-U|} allows Perl to perform unsafe operations. \Xi{|-v|} prints the version and patchlevel of your Perl executable. \Xi{|-V| [ |:| ]} prints Perl configuration information. \Xi{|-w|} prints warnings about possible spelling errors and other error-prone constructs in the script. \Xi{|-x| [ ]} extracts Perl program from the input stream. If is specified, switches to this directory before running the program. \Xi{|-0| [ ]} (that's the number zero) designates an initial value for the record separator \$|/|. See also |-l|. \end{enum} Command line options may be specified on the `|#!|' line of the perl script, except for |-M|, |-m| and |-T|. \section{Syntax} Perl is a free-format programming language. This means that in general it does not matter how the Perl program is written with regard to indentation and lines. An exception to this rule is when the Perl compiler encounters a `sharp' symbol (|#|) in the input: it then discards this symbol and everything it follows up to the end of the current input line. This can be used to put comments in Perl programs. Real programmers put lots of useful comments in their programs. There are places where whitespace does matter: within literal texts, patterns and formats. If the Perl compiler encounters the special token \_\,\_|END|\_\,\_ it discards this symbol and stops reading input. Anything following this token is ignored by the Perl compiler, but can be read by the program when it is run. \section{Variables} \begin{enum}{3.2cm} \Xi{|\$var|} a simple scalar variable. \Xi{|\$var[28]|} 29th element of array |@var|. \Xi{|\$p = \char`\\@var|} now |$p| is a reference to array |@var|. \Xi{|\$\$p[28]|} 29th element of array referenced by |$p|. Also: |$p->[28]|. \Xi{|\$var[-1]|} last element of array |@var|. \Xi{|\$var[\$i][\$j]|} |$j|-th element of |$i|-th element of array |@var|. \Xi{|\$var\{'Feb'\}|} a value from `hash' (associative array) |%var|. \Xi{|\$p = \char`\\\%var|} now |$p| is a reference to hash |%var|. \Xi{|\$\$p\{'Feb'\}|} a value from hash referenced by |$p|. Also: |$p->{'Feb'}|. \Xi{|\$\#var|} last index of array |@var|. \Xi{|@var|} the entire array; \\ in a scalar context, the number of elements in the array. \Xi{|@var[3,4,5]|} a slice of array |@var|. \Xi{|@var\{'a','b'\}|} a slice of |%var|; same as |($var{'a'},$var{'b'})|. \Xi{|\%var|} the entire hash; \\ in a scalar context, \true{} if the hash has elements. \Xj{|\$var\{'a',1,...\}|} emulates a multi-dimensional array. \Xj{|('a'..'z')[4,7,9]|} a slice of an array literal. \Xi{|::|} a variable from a package, e.g. |$pkg::var|, |@pkg::ary|. \Xi{|\char`\\|} reference to a thingie, e.g. |\$var|, |\%hash|. \Xi{|*|} refers to all thingies represented by . \\ `|*n1|~|=|~|*n2|' makes |n1| an alias for |n2|. \\ `|*n1|~|=|~|\$n2|' makes |$n1| an alias for |$n2|. \end{enum} You can always use a |{| |}| returning the right type of reference instead of the variable identifier, e.g. |${|\ldots|}|, |&{|\ldots|}|. |$$p| is just a shorthand for |${$p}|. \clearpage \section{Literals} Numeric: |123 1_234 123.4 5E-10 0xff| (hex)| 0377| (octal). \hangindent=2cm\hangafter=1 String: |'abc'| literal string, no variable interpolation nor escape characters, except |\'| and |\\|. Also: |q/abc/|. Almost any pair of delimiters can be used instead of |/|\ldots|/|. \hangindent=2cm\hangafter=1 \makebox[1cm]{}|"abc"| Variables are interpolated and escape sequences are processed. \newline Also: |qq/abc/|. \newline Escape sequences: |\t| (Tab), |\n| (Newline), |\r| (Return), |\f| (Formfeed), |\b| (Backspace), |\a| (Alarm), |\e| (Escape), |\033|(octal), |\x1b|(hex), |\c[| (control). \newline |\l| and |\u| lowcase/upcase the following character; \newline |\L| and |\U| lowcase/upcase until a |\E| is encountered. \newline |\Q| quote regexp characters until a |\E| is encountered. \hangindent=2cm\hangafter=1 \makebox[1cm]{}|`||`| evaluates to the output of the . \newline Also: |qx/||/|. \hangindent=1cm\hangafter=1 Boolean: Perl has no boolean data type. Anything that evaluates to the null string, the number zero or the string |"0"| is considered \false{}, everything else is \true{} (including strings like |"00"|!). \hangindent=1cm\hangafter=1 Array: |(1,2,3)| a three member array. |()| is an empty array. \newline |(1..4)| is the same as |(1,2,3,4)|. Likewise |('abc'..'ade')|. \newline |qw/foo bar| \ldots|/| is the same as |('foo','bar',|\ldots|)|. \hangindent=1cm\hangafter=1 Array reference: |[1,2,3]|. \hangindent=1cm\hangafter=1 Hash (associative array): |(||, ||, ||, ||, |\ldots|)|. \newline Also: |(|| => ||, || => ||, |\ldots|)|. \hangindent=1cm\hangafter=1 Hash reference: |{||, ||, ||, ||, |\ldots|}|. \hangindent=1cm\hangafter=1 Code reference: \kwd{sub} |{| |}| \hangindent=1cm\hangafter=1 Filehandles: |STDIN|, |STDOUT|, |STDERR|, |ARGV|, |DATA|. \newline User-specified: , |$|. \hangindent=1cm\hangafter=1 Globs: |<||>| evaluates to all filenames according to the pattern. \newline Use `|<${||}>|' or `\kwd{glob} \$' to glob from a variable. \hangindent=1cm\hangafter=1 Here-Is: |<<| \quad Shell-style `here document'. \hangindent=1cm\hangafter=1 Special tokens: \newline \_\,\_|FILE|\_\,\_: filename; \_\,\_|PACKAGE|\_\,\_: package; \_\,\_|LINE|\_\,\_: line number. \newline \_\,\_|END|\_\,\_: end of program; remaining lines can be read using filehandle \<\>. \clearpage \section{Operators and precedence} Perl operators have the following associativity and precedence, listed from highest precedence to lowest. \vspace{1mm} ~\begin{tabular}{@{\vline~}c@{~\vline~}l@{~\vline~}l@{~\vline}} \hline Assoc & Operators & Description\hspace{40mm} \\ \hline left & terms and list operators & See below. \\ \hline left & |->| & Infix dereference operator. \\ \hline & |++| & Auto-increment (magical on strings). \\ & |--| & Auto-decrement. \\ \hline right & |**| & Exponentiation. \\ \hline right & |\| & Reference to an object (unary). \\ right & |! ~| & Unary negation, bitwise complement. \\ right & |+ -| & Unary plus, minus.\\ \hline left & |=~| & Binds a scalar expression to a pattern match. \\ left & |!~| & Same, but negates the result. \\ \hline left & |* / % x| & Multiplication, division, modulo, repetition. \\ \hline left & |+ - .| & Addition, subtraction, concatenation. \\ \hline left & |>> <<| & Bitwise shift right, bitwise shift left. \\ \hline & named unary operators & E.g. \kwd{sin}, \kwd{chdir}, |-f|, |-M|.\\ \hline & |< > <= >=| & Numerical relational operators.\\ & \kwd{lt}~~\kwd{gt}~~\kwd{le}~~\kwd{ge} & String relational operators. \\ \hline & |== != <=>| & Numerical equal, not equal, compare. \\ & \kwd{eq}~~\kwd{ne}~~\kwd{cmp} & Stringwise equal, not equal, compare.\\ & & Compare operators return -1 (less), 0 (equal) \\ & & or 1 (greater).\\ \hline left & \& & Bitwise AND. \\ \hline left & \|| ^| & Bitwise OR, exclusive OR. \\ \hline left & \&\& & Logical AND. \\ \hline left & \|\| & Logical OR. \\ \hline & |..| & In scalar context, range operator. \\ & & In array context, enumeration. \\ \hline right & |?:| & Conditional (\textit{if} |?| \textit{then} |:| \textit{else}) operator. \\ \hline right & |= += -= *= |etc. & Assignment operators. \\ \hline left & |,| & Comma operator, also list element separator. \\ left & |=>| & Same, enforces the left operand to be a string. \\ \hline & list operators (rightward) & See below. \\ \hline right & \kwd{not} & Low precedence logical NOT. \\ \hline left & \kwd{and} & Low precedence logical AND. \\ \hline left & \kwd{or}~~\kwd{xor} & Low precedence logical OR, exclusive OR.\\ \hline \end{tabular} \vspace{2mm} Parentheses can be used to group an expression into a term. A `list' is a list of expressions, variables or lists, separated by commas. An array variable or an array slice may always be used instead of a list. All Perl functions can be used as list operators, in which case they have very high or very low precedence, depending on whether you look at the left side of the operator or at the right side of the operator. \newline Parentheses can be added around the parameter lists to avoid precedence problems. The logical operators do not evaluate the right operand if the result is already known after evaluation of the left operand. \section{Statements} Every statement is an expression, optionally followed by a modifier, and terminated with a semicolon. The semicolon may be omitted if the statement is the final one in a . Execution of expressions can depend on other expressions using one of the modifiers \kwd{if}, \kwd{unless}, \kwd{while} or \kwd{until}, e.g.: \quad \kwd{if} |;| \\ \quad \kwd{until} |;| The logical operators \|\|, |&&|, or |?:| also allow conditional execution, e.g.: \quad \|\| |;| \\ \quad |?| |:| |;| Statements can be combined to form a when enclosed in |{}|. s may be used to control flow: \quad \kwd{if} |(||)| [ [ \kwd{elsif} |(||)| ] \kwd{else} ] \\ \quad \kwd{unless} |(||)| [ \kwd{else} ] \\ \quad [