|
Edmund J. Sutcliffe
Thoughtful Solutions, Creatively Implemented and Communicated
Edmund's Perl Quick Reference -
1
2
3
4
5
6
7
8
9
Click here
for a printable version of this page.
Subroutines, Packages and Modules
- &SUBROUTINE
LIST
- Executes a SUBROUTINE declared by a sub
declaration, and returns the value of the last expression
evaluated in SUBROUTINE. SUBROUTINE can be an expression
yielding a reference to a code object. The &
may be omitted if the subroutine has been declared before
being used.
- bless REF [ , PACKAGE
]
- Turns the object REF into an object in PACKAGE.
Returns the reference.
- caller [ EXPR ]
- Returns an array ($package,$file,$line,...)
for a specific subroutine call. caller returns
this info for the current subroutine, caller(1)
for the caller of this subroutine, etc. Returns false
if no caller.
- do SUBROUTINE LIST
- Deprecated form of &SUBROUTINE.
- goto &SUBROUTINE
- Substitutes a call to SUBROUTINE for the current
subroutine.
- import MODULE [ [ VERSION ] LIST ]
- Imports the named subroutines from MODULE.
- no MODULE [ LIST ]
- Cancels imported semantics. See use.
- package NAME
- Designates the remainder of the current block
as a package.
- require EXPR†
- If EXPR is numeric, requires Perl to be at
least that version. Otherwise EXPR must be the name of a
file that is included from the Perl library. Does not include
more than once, and yields a fatal error if the file does
not evaluate to a true value. If EXPR is a bare word,
assumes extension .pm for the name of the
file.
- return EXPR
- Returns from a subroutine with the value specified.
- sub NAME { EXPR ;
... }
- Designates NAME as a subroutine. Parameters
are passed by reference as array @_. Returns
the value of the last expression evaluated.
- [ sub ] BEGIN {
EXPR ; ... }
- Defines a setup BLOCK to be called before execution.
- [ sub ] END {
EXPR ; ... }
- Defines a cleanup BLOCK to be called upon termination.
- tie VAR, PACKAGE,
[ LIST ]
- Ties a variable to a package that will handle
it. Can be used to bind a dbm or ndbm file to a hash.
- untie VAR
- Breaks the binding between the variable and
the package.
- use MODULE [ [ VERSION ] LIST ]
- Imports semantics from the named module into
the current package.
Object-Oriented Programming
Perl rules of object oriented programming:
- An object is simply a reference that happens
to know which class it belongs to. Objects are blessed,
references are not.
- A class is simply a package that happens to
provide methods to deal with object references. If a package
fails to provide a method, the base classes as listed in
@ISA are searched.
- A method is simply a subroutine that expects
an object reference (or a package name, for static methods)
as the first argument.
Methods can be applied with:
METHOD OBJREF PARAMETERS or
OBJREF->METHOD PARAMETERS
Arithmetic
Functions
| abs EXPR† |
Returns the absolute
value of its operand. |
| atan2 Y,X |
Returns the arctangent
of Y/X in the range -pi to pi. |
| cos EXPR† |
Returns the cosine
of EXPR (expressed in radians). |
| exp EXPR† |
Returns e
to the power of EXPR. |
| int EXPR† |
Returns the integer
portion of EXPR. |
| log EXPR† |
Returns natural
logarithm (base e) of EXPR. |
| rand [
EXPR ] |
Returns a random
fractional number between 0 and the value of EXPR. If
EXPR is omitted, returns a value between 0 and 1. |
| sin EXPR† |
Returns the sine
of EXPR (expressed in radians). |
| sqrt EXPR† |
Returns the square
root of EXPR. |
| srand
[ EXPR ] |
Sets the random
number seed for the rand operator. |
| time |
Returns the number
of seconds since January 1, 1970. Suitable for feeding
to gmtime and localtime. |
Conversion Functions
- chr EXPR†
- Returns the character represented by the decimal
value EXPR.
- gmtime EXPR†
- Converts a time as returned by the time
function to a 9-element array (0: $sec,
1: $min, 2: $hour,
3: $mday, 4: $mon,
5: $year, 6: $wday,
7: $yday, 8: $isdst)
with the time analyzed for the Greenwich time zone. $mon
has the range 0..11 and $wday has the range
0..6.
- hex EXPR†
- Returns the decimal value of EXPR interpreted
as a hex string.
- localtime EXPR†
- Converts a time as returned by the time
function to ctime(3) string. In array context, returns
a 9-element array with the time analyzed for the local time
zone.
- oct EXPR†
- Returns the decimal value of EXPR interpreted
as an octal string. If EXPR starts off with 0x,
interprets it as a hex string instead.
- ord EXPR†
- Returns the ASCII value of the first character
of EXPR.
- vec EXPR, OFFSET,
BITS
- Treats string EXPR as a vector of unsigned
integers, and yields the bit at OFFSET. BITS must be between
1 and 32. May be assigned to.
Structure Conversion
pack TEMPLATE, LIST
Packs the values into a binary structure using TEMPLATE.
unpack TEMPLATE, EXPR
Unpacks the structure EXPR into an array, using TEMPLATE.
TEMPLATE is a sequence of characters as follows:
| a / A |
ASCII string, null- / space-padded |
| b / B |
Bit string in ascending /
descending order |
| c / C |
Native / unsigned char value |
| f / d |
Single / double float in
native format |
| h / H |
Hex string, low / high nybble
first |
| i / I |
Signed / unsigned integer
value |
| l / L |
Signed / unsigned long value |
| n / N |
Short / long in network (big
endian) byte order |
| s / S |
Signed / unsigned short value |
| u / P |
Uuencoded string / pointer
to a string |
| v / V |
Short / long in VAX (little
endian) byte order |
| x / @ |
Null byte / null fill until
position |
| X |
Backup a byte |
Each character may be followed by a decimal
number that will be used as a repeat count; an asterisk (*)
specifies all remaining arguments. If the format is preceded
with %N, unpack returns an N-bit checksum instead.
Spaces may be included in the template for readability purposes.
|
|