Difference between revisions of "Perl reference"
Jump to navigation
Jump to search
m (→Special Variables: some more vars) |
(→Special Variables: and hopefully that's all) |
||
Line 83: | Line 83: | ||
| '''%ENV''' || hash of all environment variables (see http://vbz.net/cgi-bin/env for a sample listing) | | '''%ENV''' || hash of all environment variables (see http://vbz.net/cgi-bin/env for a sample listing) | ||
|- | |- | ||
− | | '''$_''' || default argument for many functions | + | | '''$_''' || The default input and pattern-searching space; default argument for many functions. |
|- | |- | ||
| '''@_''' || list of arguments passed to subroutine (usually in parentheses) | | '''@_''' || list of arguments passed to subroutine (usually in parentheses) | ||
|- | |- | ||
− | | '''$0''' || name of Perl program file (the outermost one, | + | | '''$0''' || name of Perl program file (the outermost one; may or may not include path, depending on system (in Linux, it does not)) |
|- | |- | ||
| '''$]''' || version number of the Perl interpreter | | '''$]''' || 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 (may not be very useful for CGI because Apache always executes as the same user) | | '''$<''' || username of user running the Perl script (may not be very useful for CGI because Apache always executes as the same user) | ||
Line 95: | Line 97: | ||
| '''$^X''' || [[filespec]] of the Perl interpreter | | '''$^X''' || [[filespec]] of the Perl interpreter | ||
|- | |- | ||
− | | '''$/''' || string to be used as input record separator | + | | '''$/''' || string to be used as input record separator; default = newline |
+ | |- | ||
+ | | '''$\''' || string to be used as output record separator in [[print (Perl function)|print]] | ||
+ | |- | ||
+ | | ''';''' || The subscript separator for multi-dimensional array emulation. | ||
+ | |- | ||
+ | | '''$.''' || The current input line number (or record number) of the last filehandle that was read. | ||
+ | |- | ||
+ | | '''$,''' || 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 (Perl function)|print]] | ||
+ | |- | ||
+ | | '''$$''' || The process number of the Perl running this script | ||
+ | |- | ||
+ | | '''$?''' || The status returned by the last 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. | ||
+ | | colspan=2 | | ||
+ | ===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 | ||
+ | |- | ||
+ | | colspan=2 | | ||
+ | ===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 | ||
+ | |- | ||
+ | | '''$^''' || Name of the current top-of-page format | ||
+ | |- | ||
+ | | colspan=2 | | ||
+ | ===Pattern Matching (regex)=== | ||
+ | These are associated with the last successful pattern match. | ||
+ | |- | ||
+ | | '''$<u>n</u>''' || in the replacement section, indicate that the <u>n</u><sup>th</sup> 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 |
|- | |- | ||
− | | '''$ | + | | '''$`''' || The string preceding whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already. |
|- | |- | ||
− | | '''$ | + | | '''$'''' || The string following whatever was matched by the last pattern match, not counting patterns matched in nested blockes that have been exited already. |
|- | |- | ||
− | | '''$ | + | | '''$+''' || 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. |
|- | |- | ||
− | | | + | | |
|} | |} | ||
+ | Portions of this were shamelessly copied from [http://www.kichwa.com/quik_ref/spec_variables.html here], at least until more information can be found. |
Revision as of 00:44, 13 March 2006
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 | 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 |
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) | ||
$_ | The default input and pattern-searching space; default argument for many functions. | ||
@_ | list of arguments passed to subroutine (usually in parentheses) | ||
$0 | name of Perl program file (the outermost one; may or may not include path, depending on system (in Linux, it does not)) | ||
$] | 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 (may not be very useful for CGI because Apache always executes as the same user) | ||
$^X | filespec of the Perl interpreter | ||
$/ | string to be used as input record separator; default = newline | ||
$\ | string to be used as output record separator in print | ||
; | The subscript separator for multi-dimensional array emulation. | ||
$. | The current input line number (or record number) of the last filehandle that was read. | ||
$, | 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 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 | ||
ReportingThese 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 | ||
$^ | Name of the current top-of-page format | ||
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 | ||
$` | The string preceding whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already. | ||
$' | The string following whatever was matched by the last pattern match, not counting patterns matched in nested blockes that have been exited already. | ||
$+ | 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. | ||
Portions of this were shamelessly copied from here, at least until more information can be found.