# Perl Programming/Function reference
## String functions
### chomp
#### Action

Removes the last characters from a string only if they're recognized as a record separator (e.g. a newline character)

#### Syntax

chomp($String = $_);

#### See also

* chop - To remove the last character from a string

### chop
#### Action

Removes the last character from a string regardless

#### Syntax

chop($String = $_);

#### See also

* chomp - To remove the last character from a string if it is a record seperator
Removes the last character from a string (e.g. removes the newline characters when reading from a file)

### chr

Gets an ASCII character, given it's code

### crypt

(See also MD5 )
The salt string needs only be two characters long, and provides a way of randomising the hash, such that the same word can produce several different hashes, if used with different values of $Salt;!

### hex

Converts a number to hexadecimal
Other way around - converts hex to number: print hex(11); # prints 17
you can use
print sprintf("%X",11); # Prints B

### index

Search for one string within another (see rindex to search from end-to-start).
The special variable $[ always gets added to the return value, but $[ is normally 0, and the manual recommends leaving it at 0.

### lc

Converts a string to lower-case

### lcfirst

Converts the first character of a string to lowercase

### length

Returns the length of a string

### oct

Converts a number to octal

### ord

Converts a character to its number.

### pack

Takes a list and converts it into a string using a supplied set of rules.
$Template can be made up of:
Each letter may optionally be followed by a number giving a repeat count.
The integer types s, S, l, and L may be immediately followed by a ! suffix to signify native shorts or longs

### reverse

Reverses a string (in scalar context) or a list (in list context):

### rindex

Search for one string within another, starting at the end of the string.

### sprintf

Prints a formatted string:
Use %% to get a percent-sign.
Use %n to request the number of characters written so far, and put it into the next variable in the list. You may want to check that user-supplied formatting rules don't contain this code.

### substr

Return part of a string (a substring)
Format: substr string start-position length
    start-position is zero-based.
    A negative number starts from the end of the string.
You can use substr on the left side of an assignment statement to change part of a string. This can actually shorten or lengthen the string.

### uc

Converts a string to upper-case

### ucfirst

Converts the first character of a string to uppercase

## Numeric functions
### abs

Returns the absolute (positive) value of a number

### int

Rounds a number towards zero, returning an integer

### sqrt

See the Math::Complex module, if you need to take roots of negative numbers.

### srand

Version-dependent, and older versions of Perl are not guaranteed to have a good seed value. See the Math::TrulyRandom module for more possibilities. The current version of Perl uses the urandom device if it's available.

## Array functions
### pop

Take the last element from an array.

### push

Push a list of elements onto the end of an array.

### shift

Take the first element out of an array.

### unshift

Add a list of elements onto the beginning of an array.

## List functions
### unpack

Unpacks a string into a list - see the templates available for the pack function for details

## Input and output functions
### binmode
### die

Exits the program, printing to "STDERR" the first parameter and the current file and line. Used to trap errors.

### eof

This function returns true, if the next read on FILEHANDLE would return end-of-file, or if FILEHANDLE is not open. FILEHANDLE may be an expression whose value gives the real filehandle, or a reference to a filehandle object of some sort. An eof without an argument returns the end-of-file status for the last file read. An eof with empty parentheses tests the ARGV filehandle (most commonly seen as the null filehandle in <>). Therefore, inside a while (<>) loop, an eof with parentheses will detect the end of only the last of a group of files. Use eof (without the parentheses) to test each file in a while (<>) loop. For example, the following code inserts dashes just before the last line of the last file:
On the other hand, this script resets line numbering on each input file:
Like "$" in a sed program, eof tends to show up in line number ranges. Here's a script that prints lines from /pattern/ to end of each input file:
Here, the flip-flop operator (..) evaluates the pattern match for each line. Until the pattern matches, the operator returns false. When it finally matches, the operator starts returning true, causing the lines to be printed. When the eof operator finally returns true (at the end of the file being examined), the flip-flop operator resets, and starts returning false again for the next file in @ARGV

### print

Prints the parameters given.
Discussed in the following sections:
    Digression on print in Strings section

### write
## Functions for working with fixed length records
### pack

See the entry for pack further up the page

### vec
## Filesystem functions
### chroot

Sets the root directory for the program, such that the "/" location refers to the specified directory.
Program must be running as root for this to succeed.

### fcntlglob

See also File::Glob.

### lstat

Identical to stat, except that if given file is symbolic link, stat link not the target.

### rename

May work differently on non-*nix operating systems, and possibly not at all when moving between different filesystems. See [[File::Copy]] for more complicated file operations.

### utime
## Program functions
### caller

Returns information about the current function call stack. In scalar context, returns only the name of the package from where the current subroutine was called. In list context, returns the package, filename, and line number. In list context with a numeric argument passed, returns several pieces of information (see below). The argument represents how many levels in the call stack to go back.

### import

There is no actual 'import' function. Rather, it is a convention when writing a module to create a subroutine named 'import' that populates the current namespace with that module's needed variables or methods.
The standard 'Exporter' module provides an import method, if your class has it as a base class.

### package

Declares all lines that follow (until EOF or the next package statement) to belong to the given package's namespace.

### require

includes the specified module's code into the current program. The module can be specified either with an absolute or relative path, or with a bareword. If a bareword is given, a '.pm' extention is added, and :: is replaced with the current operating system's path seperator:

### use

Requires and imports the given module or pragma, at compile time. The line
is identical to

## Classes and objects

See also Perl Objects

### use
## Sockets
### socketpair
## Login information
### setpwent
## Network information
### setservent
## Time and date
### gmtime

Converts a timestamp to GMT.

### localtime

Converts a timestamp to local time.

### time

Returns number of seconds since an epoch (that is system-dependent, but may be 1970-01-01).
See also Time::Hires

### times
## Functions that reverse each other

Some functions in perl reverse or otherwise cancel the effect of each other, so running a string through both of them will produce the same output as the input, for example
will echo 1 to standard output,
ord will convert a character to its number in the character set, while chr will convert a number to its corresponding character, therefore
in the same way that ${\sqrt {x^{2}}}=x$ and ${\sqrt {x}}^{2}=x$ in Mathematics (assuming x is non-negative), ord(chr(1)) = 1 and chr(ord(1)) = 1 in Perl.
List of functions that reverse each other:
* lc and uc
* lcfirst and ucfirst
* ord and chr
* join and split
* push and pop
* unshift and shift