Perl reference

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Revision as of 23:04, 7 April 2006 by Woozle (talk | contribs) (→‎Escape Sequences: example of \cn)
Jump to navigation Jump to search

Reference for various things in Perl. See also Perl built-in functions.

Escape Sequences

\a bell (ctrl-G, 007 decimal)
\b backspace (ctrl-H, 008 decimal)
\cn ctrl-n (e.g. \cj = CTRL-J, i.e. Line Feed)
\e ESC (027 decimal, 033 octal)
\f FF
\l converts next letter to lowercase
\n newline - system-dependent (CRLF on DOS/Win)
\r CR (013 decimal)
\t TAB (ctrl-I, 009 decimal)
\u converts next letter to uppercase
\L converts all characters to lowercase, from here to next \E
\U converts all characters to uppercase, from here to next \E
\E ends case conversion started by \L or \U
\' prints a literal single-quote
\" prints a literal double-quote
\$ prints a literal dollar sign
\\ prints a literal backslash (not doubled)
\0nnn prints the ASCII character numbered nnn in octal
\xnn prints the ASCII character numbered nn in hexadecimal

Operators

Arithmetic

+ add $x+$y
- subtract $x-$y
* multiply $x*$y
/ divide $x/$y
% modulo (remainder) $x%$y -> remainder of x/y
** exponent $x**$y -> xy
++ incrememt $x++ or ++$x -> x is incremented by 1
-- decrement $x-- or --$x -> x is decremented by 1

Comparisons

== is equal $x==$y returns true iff x equals y
!= is different $x!=$y returns true iff x does not equal y
< is less than $x<$y returns true iff x is smaller than y
> is more than $x>$y returns true iff x is larger than y

Strings

. concatenate $x.$y combines the strings in x and y
eq strings are equal $xeq$y returns true iff the two strings are identical
ne strings are different $xne$y returns true iff the two strings are different

Other

?: conditional result $a?$b:$c returns $b if $a is true, otherwise returns $c

File Test Operators

All operators are used like this:

-x $filename
-r Is the file readable?
-w Is the file writable?
-x Is the file executable?
-e Does the file exist?
-z Is the file empty? (i.e. zero bytes)
-s File length in bytes
-f Is the file an ordinary file?
-d Is the file a directory?
-l Is the file a symbolic link? (UNIX/Linux only)
-p Is file a named pipe?
-S Is the file a socket?
-T Is the file a text file?
-B Is the file a binary file? (!-T)
-M Number of days since file was last modified
-A Number of days since file was last accessed

Special Variables

@ARGV array of command-line arguments with which Perl was invoked
%ENV hash of all environment variables (see http://vbz.net/cgi-bin/env for a sample listing)
$0 name of Perl program file (the outermost one; may or may not include path, depending on system (in Linux, it does not))
$^A current value of the accumulator used by the write() function
$^D current value of Perl's debugging flags
$^F maximum system file descriptor (typically 2) not sure what this means
$^L what characters are printed to accomplish a form feed
$^T time at which the script began running, in seconds since 00:00:00 GMT January 1, 1970
$^W is true if warnings have been turned on (by running Perl with the -w command option)
$^X filespec of the Perl interpreter
$_ The default input and pattern-searching space; default argument for many functions.
@_ list of arguments passed to subroutine (usually in parentheses)
$) effective group ID of the current process (UNIX/Linux)
$( real group ID of the current process (UNIX/Linux)
$] version number of the Perl interpreter
$[ The index of the first element in an array, and of the first character in a substring.
$< username of user running the Perl script real user ID of the current process (UNIX/Linux) (may not be very useful for CGI because Apache always executes as the same user)
$> effective user ID of the current process (UNIX/Linux)
$/ string to be used as input record separator; default = newline
$\ string to be used as output record separator in print()
$: "soft-hyphen" characters, i.e. the set of characters after which a line may be broken to fill continuation fields (starting with ^) in a format. Default is space, newline, hyphen.
$; The subscript separator for multi-dimensional array emulation.
$. The current input line number (or record number) of the last filehandle that was read.
$, output field separator for print() function, i.e. the separator for printing arrays
$" This is similar to "$," except that it applies to array values interpolated into a double-quoted string (or similar interpreted string)
$# The output format for numbers display via print()
$$ The process number of the Perl running this script
$? The status returned by the last child process, i.e. pipe close, backtick(``) command or system operator.
$! If used in a numeric context, yields the current value of errno, with all the usual caveats. (This means that you shouldn't depend on the value of $! to be anything in particular unless you've gotten a specific error return indicating a system error.) If used in a string context, yields the corresponding sysem error string.
$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

Output Flags

If set to nonzero, forces a flush after every write or print
$* If set to zero, pattern-matching assumes that strings contain a single line (for optimization purposes). Default = 0

Reporting

These refer to the currently selected filehandle; each filehandle maintains its own set of variables.

$% Current page number
$= Current page length
$- Number of lines remaining on the page
$~ Name of the current report format (default is the filenandle's name)
$^ Name of the current top-of-page format (default is the filehandle's name plus "_TOP")

Pattern Matching (regex)

These are associated with the last successful pattern match.

$n in the replacement section, indicate that the nth matched section in parentheses should be used here (first matched parentheses becomes $1, second becomes $2, etc., up through $9)
$& Contains the string matched by the last pattern match. Introduces CPU/memory overhead for all regex operations in the script; avoid.
$` The string preceding whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already. Introduces CPU/memory overhead for all regex operations in the script; avoid.
$' The string following whatever was matched by the last pattern match, not counting patterns matched in nested blockes that have been exited already. Introduces CPU/memory overhead for all regex operations in the script; avoid.
$+ The last bracket matched by the last search pattern. This is useful if you don't know which of a set of alternative patterns matched.
$* When set to 1, string searching is done for multiple lines within the string (i.e. if the string contains newline characters); otherwise, presumably the search stops at the first newline.

Portions of this were shamelessly copied from here, at least until more information can be found. Other portions were copied from Discover Perl 5 by Naba Barkakati, ISBN 0-7645-3076-3