Perl Information Center Tutorials - Reference
These tutorials were written to help you get a quick, but thorough, understanding of Perl -
the scope of the language as well as it's specific capabilities.
| Beginners
| Built-In Functions
| Advanced
| CGI Applications
|
|
|
|
|
|
Reference
Sometimes I don't want to read through the whole manual. I just want
to get a single piece of information and get back to my programming. That
is what this page is about. Everything on this page is in summary form -
short and to the point. Once you've gone through all the tutorials at this
site, this is the page you'll return to for those quick reminders!
Operators
----------------------------------------------------------------------
+ += and & && < lt '' =~ ->
- -= or | || > gt "" !~ =>
* *= xor ^ <= le `` # <<
/ /= not ! >= ge q/ / .. >>
% %= == eq qq/ / . <>
** **= != ne qr/ / .= \
++ &= <=> cmp qx/ / x ?:
-- |= qw/ / x=
= ^=
<<=
>>=
&&=
\\=
Arrays/Lists Hashes Strings
---------------- ---------- ---------------------------
grep reverse delete chomp ord substr
join shift each chop pack tr///
map sort exists chr q// y///
pop splice keys crypt qq// uc
push unpack values index reverse ucfirst
qw// unshift lc rindex
lcfirst sprintf
length
Error Handling Database System Objects
-------------- -------- ----------------- ---------
die dbmClose defined sleep bless
dump dbmOpen exec system ref
eval tie exit reset tied
warn untie reset undef
scalar wait
Subroutines Scope Time Math Regular Expressions
----------- --------- ---------- --------------- -------------------
caller import gmtime abs log m//
prototype local localtime atan2 oct s///
return my time cos rand pos
sub no times exp srand qr//
wantarray our hex sin quotemeta
package int sqrt split
require study
use
Files
--------------------------------------------------------------------
Open Read Write Properties Links Misc
--------------------------------------------------------------------
binmode getc print chmod link fcntl
close read printf chown lstat ioctl
eof readline format fileno readlink syscall
open seek formline flock symlink sysread
select tell truncate rename sysseek
unlink write stat syswrite
umask sysopen
utime vec
-X
Flow Constructs Directories
--------------------------------------- ---------------------
do {..statements..} if (expression) chdir readdir
do {..statements..} unless (expression) chroot rewinddir
do {..statements..} while (expression) closedir rmdir
do {..statements..} unitil (expression) glob seekdir
mkdir telldir
if (expression) {..statements..} opendir
unless (expression) {..statements..}
while (expression) {..statements..}
unitil (expression) {..statements..}
{..statements..} if (expression)
{..statements..} unless (expression)
{..statements..} while (expression)
{..statements..} until (expression)
(expression) ? (..true statements..) : (..false statements..)
map {...} (list)
grep {...} (list)
goto LABEL
foreach $var (list) {...} # list members put in $var
foreach (list) {..statements..} # list members put in $_
for ( initvalue; test; increment) {...}
if (expression) {...}
elsif (expression) {...}
elsif (expression) {...}
else {...}
Flow control - redo, continue, next, last:
while (EXPR) {
# redo always comes here
do_something;
} continue {
# next always comes here
do_something_else;
# then back the top to re-check EXPR
}
### last always comes here
Escape Characters
- \n = newline - positions the next print position at the beginning of the next line
- \' - single quote - insert a single quote character into a single-quoted string
- \" - double quote - insert a double quote character into a single-quoted string
- \b - backspace - backspaces and erases the last character
- \216 - octal value - octal value corresponding to a decimal character
- \x8E - hex value - hex value corresponding to a decimal character
- \\ - backslash - insert a backslash (otherwise interpreted as introducing a control character)
- \r - return - positions next print position at the beginning of the same line
- \t - tab - moves to the next tab stop
- \f - formfeed - advance paper to top of next page
- \a - bell - rings the terminal's bell/beep
- \e - escape - used to introduce terminal-specific control sequence
- cC - control character - Control-C on the keyboard
Pre-Defined Variables
Environment Variables
This is a list of CGI 1.1 environment variables. These variables are set automatically
when a CGI script is called.
- AUTH_TYPE
Contains the type of authentication being used to limit access to the current document. E.g. "Basic" or "Digest".
- CONTENT_LENGTH
Contains the length of information provided by a client POST. The length is an ASCII string representing the number of bytes.
- CONTENT_TYPE
Contains the content type supplied by the client with POSTed data.
- GATEWAY_INTERFACE
Contains "CGI/1.1".
- HTTP_ACCEPT
Contains the contents of any "Accept:" headers supplied by the client.
- HTTP_ACCEPT_LANGUAGE
Contains the contents of any "Accept-Language:" headers supplied by the client.
- HTTP_ACCEPT_CHARSET
Contains the contents of any "Accept-Charset:" headers supplied by the client.
- HTTP_COOKIE
Contains the contents of any "Cookie:" header supplied by the client.
- HTTP_FROM
Contains the contents of the "From:" header supplied by the client. This may contain the e-mail address of the client user. This is generally unreliable, as usually users choose not to supply this information. If they do give it they can choose any e-mail address they want -- there is no guarantee that this is, in fact, the real e-mail address of the client user.
- HTTP_HOST
Contains the contents of the "Host:" header supplied by the client. This should contain the one of the aliases for the host on which the server is running. It should be the hostname from the URL that the client is requesting. Thus a client seeking http://www.serverhost.com:8000/foo.html should supply "www.serverhost.com" in this header. Many browsers do not do this. It is required in HTTP/1.1.
- HTTP_RANGE
Contains the contents of any "Range:" header supplied by the client.
- HTTP_REFERER
Contains the contents of the "Referer:" header supplied by the client. This is supposed to contain the URL of the document from which a link to this script activated.
- HTTP_USER_AGENT
Contains the contents of the "User-Agent:" header supplied by the client. This is supposed to contain the name of the browser which the client is using to make this request
- PATH_INFO
Extra information in the "path" of a CGI URL. More precisely everything after the actual name of the script. E.g. the URL http://host/dir/foo.cgi/stuff1/stuff2/stuff3 would have /stuff1/stuff2/stuff3 in the PATH_INFO variable.
- PATH_TRANSLATED
This assumes that PATH_INFO is the path of a file relative to your WN root and provides the path relative to the system root. E.g. the URL http://host/dir/foo.cgi/stuff1/stuff2/stuff3 would have /path/to/WN_root/stuff1/stuff2/stuff3 in the PATH_TRANSLATED variable.
- QUERY_STRING
The contents of the "query" part of an HTTP GET request. This is whatever comes after the '?' in the URL. E.g. the URL http://host/dir/foo.cgi/stuff1/stuff2/stuff3?dingbat would have dingbat in the QUERY_STRING variable. If the request was generated by a form this contains all the form information encoded as name1=value1&name2=value2...
- REMOTE_ADDR
Contains the IP address of the client or proxy making the request.
- REMOTE_HOST
Contains the hostname of the client or proxy making the request, if the server can determine it otherwise it contains the IP address. If NO_DNS_HOSTNAMES is defined in config.h before compilation then no attempt will be made to determine the hostname and REMOTE_HOST will contain the IP address. (This would reduce the load on the server).
- REMOTE_IDENT
Contains the user name supplied by an RFC931 identd server on the client (if there is one and you have compiled the server with RFC931 services enabled in the config.h file.)
- REMOTE_USER
If the CGI script is password protected this will contain the username provided by the client.
- REQUEST_METHOD
Contains either "GET", "POST" or "PUT", depending on the "method" of the client request.
- SCRIPT_NAME
The name of the CGI script being executed and its path relative to the WN root. E.g. the URL http://host/dir/foo.cgi/stuff1/stuff2/stuff3 would have /dir/foo.cgi in the SCRIPT_NAME variable
- SERVER_SOFTWARE
Contains "WN ", e.g. "WN 1.12.3". Recall that version numbers of the form a.b.c with 'b' even are intended to be stable releases while 'b' odd indicates a release with new and less tested features.
- SERVER_NAME
Contains the name of the "virtual host" currently being accessed, if virtual hosting is in use. Otherwise it contains the name of host on which server is running. The value of this variable will be identical to the value of HTTP_HOST if the client supports the Host header. Otherwise it will be determined by the IP address to which the request was made and the corresponding entry in the virtual hosts table.
- SERVER_PROTOCOL
Contains "HTTP/0.9" or "HTTP/1.0" depending on which protocol the client is using.
- SERVER_PORT
Contains port on which server is running.
File Test Operators
These -X functions provide information about files.
Syntax:
-X FILEHANDLE
Example:
open (INFILE, "myfile.txt");
$size = -s INFILE; # $size is byte size of file
Test Operators:
-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).
|