Some Built in Perl Functions

chdir
Takes one string argument and will change the current working directory to it. (Returns 1 on success and 0 on failure.)
chmod
Takes at least one argument. The first is a file protection mode and the rest are the names of the files that are to have their protections changed. (Returns the number of files actually changed.)
chop
Takes the last character off a string. Defaults to $_ if no arguments are given. (Returns the character removed.)
chown
Takes at least two arguments. The first is the owner and the second is the group. The rest are the files to be modified. The owner and group must be numerical. (Returns the number of files actually changed.)
close
Closes a filehandle. (If it is open.)
defined
Checks to see if its argument is defined. Variables, in perl, pop into existance when you use them. So, this function returns true if the variable has previously been used and false otherwise. You can use this on hash tables to see if values in the hash table are defined.
delete
Use this to delete entries from a hash table. These entries will then cause defined (and exists) to show up false.
die
Acts like print, but outputs to STDERR and exits the program. Use this to output an error message when you do not with the program to continue thereafter.
each
Takes a hash and returns the ``next'' hash table entry each time it is called. Use this to process each entry of a hash table, one at a time. (It returns an array of two elements. The first is the key, the second is the value.)
eval
Will take its argument and ``run'' it like it were a mini perl program. If there is no argument, it will evaluate $_. This is very dangerous to use in a CGI program!
exec
Will take its argument(s) and ``run'' it like it were a Unix command. It will replace the current running perl program with this new command. (See system.) Be very careful when using this in CGI programs.
exists
Will return true if the given has table entry exists in the hast table. If not, it returns false. (Deleteed extries will not exists. Undefed extries may.)
exit
Exits the perl program. The exit value can be given as an argument. If there is no argument, the exit value is zero.
getc
Gets the next character from the given filehandle, STDIN if the filehandle is omitted. This is a really slow way to process a file. Use if at all possible.
grep
Takes at least two arguments. The first is an expression that is evaluated on each of the subsequent arguments. (Each time, setting $_ to the element being processed.) If you write the program expecting an array, it lists all the elements that corresponded to evaluating to true. If you write the program expecting a number, you get the number of times the first argument evaluated true.

This is a very useful function that can be used to do many unexpected things since you modify arguments when you modify $_. Also see map.

index
Returns the character position of the second string argument in the first. (Character counts start at 0 and it returns -1 if no occurance is found.)
join
Connects, with the first argument, all subsequent arguments.
keys
Given a hash table as an argument, it returns an array of all the keys in that hash table. (Also see values.)
last
Exits a loop before its end.
length
Returns the length of a given string. (If there is no argument, $_ is used.)
localtime
Turns a Unix time number into an array representing the time/date in the local time zone. Like this: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
map
Does the same as grep but returns the whole array.
mkdir
Creates a directory namedthe first argument giving it the Unix file protection expressed by the second argument. Like this: mkdir 'mydir', 02755;
next
Causes perl to immediately go back to the top of the surrounding loop.
open
Allows you to open new filehandles. Open uses Unix shell characters to express how the file should be opened. For example: # This creates a new file for output. If the file already exists, # the old file is deleted. open(FILE, '>filename'); # This appends output to an existing file. If the file does not exist, # it is created. open(FILE, '>>filename'); # This opens a file for reading. open(FILE, 'filename'); # This starts a program and makes output through FILE go to # that program's STDIN. open(FILE, '|prog'); # This starts a program and makes input through FILE come from # that program's STDOUT open(FILE, 'prog|'); The FILE can then be used as a new filehandle, just like STDIN and STDOUT. Open returns an undefined value if it fails to work. Open is another funtion that should be used carefully in CGI programs.
pack (and unpack)
These are very useful functions for cramming data into special formats and extracing it back out again. The use of these functions is rather complicated, however. If you really want to know their details, you can look them up.
pop
Chop the last element of the given array off the array and return it. (Opposite of push.)
print
Prints its list of arguments to STDOUT. If the first argument is a filehandle, it will print to the filehandle instead.
push
Crams the second argument on the end of the array (as a new element) given as its first argument. (Opposite of pop.)
read
Reads from the filehandle given as its first argument, putting the result in the scalar variable given as a second argment the number of characters given in the third argument.
rename
Changes the name of the file in the first argument to the name in the second argument.
return
End the execution of the user defined function and return the argument as the result.
reverse
Reverse the given array's elements.
rindex
Like index but backwards.
rmdir
Remove the given Unix directory. (As long as it is empty.)
s/regular expression/replacement text/
This is the perl substitution operator. It replaces any text that matches the regular expression with the replacement text. You can put it on the right hand side of a =~ to have it work on a particular scalar of your choice or leave it by itself to operate on $_.

This is a very useful operator, so I will go into it somewhat.

If used like this:

$text = 'Subject: What does subject: mean?'; $text =~ s/[Ss]ubject: /The subject is: /; print $text, "\n"; The subject is: What does Subject: mean?; the first occurance of the string ``subject'' with a colon following it is replaced by the text ``The subject is: ''.

You can modify the substitution operator by putting letters after the final slash. For example, putting a ``g'' after the last slash will make the substitution work for every occurance in the text. (Not just the first one.)

$text = 'Subject: What does subject: mean?'; $text =~ s/[Ss]ubject: /The subject is: /g; print $text, "\n"; The subject is: What does The subject is: mean?;

You could do a pattern match without case sensitivity by using ``i'' like this:

$text = 'Subject: What does subject: mean?'; $text =~ s/subject: /The subject is: /i; print $text, "\n"; The subject is: What does Subject: mean?;

You can also use the $n variables, if you wish:

$text = 'Subject: What does Subject: mean?'; $text =~ s/^subject: (.*)$/The subject is: ``$1''/i; print $text, "\n"; The subject is: ``What does subject: mean?''

There are a couple of other modifiers that you might find useful:
e

This causes the replacement text to be evaluated like it were an expression. This is very useful for translating text encodings. For example, in HTML, URLs have a restricted syntax. If you wish to send certain characters, like space and slash, as text they need to be translated to a %hex notation. Here is a single line of perl that will decode such encodings:

$decodeme =~ s/%([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;

In this case, everything in $decodeme that matches a percent followed by two hex characters will be handed to the expression on the right. In this case, hex interprets the two characters together as a hex number into the machine representation of the number. This number is handed to chr, which turns interprets that number as an ASCII value and turns it into a character. This character replaces the percent-two-hex text that matched and the next match is sought.

m

Treat the string as if it were multiple lines. Normally, it is assumed that the end of the line is also the end of the string, so $ always matches the end of the string. This changes pattern matches to seek out a \n character for the end of a line to match $.

s

Treat the string as if it were a single line. This is the default behavior.

o

Complie pattern once.

Okay, I have not told you everything about regular expressions. You see, they are even more complicated than they seem. You type them in and perl has to compile them into some internal form with which it can operate.

Normally, perl can tell that evey time it encounters your regular expression, it is going to be the same. When it knows this, it compiles the regular expression and keeps that compiled version around to use again.

Sometimes, perl doesn't know, like when you have a variable in the regular expression. In this case, perl will recompile the regular expression every time it is excountered. You can use ``o'' to force perl to compile once and never again.

This is intended as a way for you to help perl run faster. Unless you are doing something that is using regular expressions in a very time critical way, you should not use this. Even if you think you need it, make sure you are using it right and test your code!

x

Use extended regular expressions. Don't ask.

select
Choose the filehandle that print and others use by default.
shift
Like pop but removes the first element in the given array instead of the last. (If no array is given, it will use @ARGV in the main program and @_ in user defined functions.)
sort
Sort the arguments. (Usually an array.) There is also the capability of providing a function to help it decide what elements are larger or smaller than others.
splice
Does more elaborate forms of push, pop, shift and unshift. With splice you can add or cut out sections of an array.
split
Uses the regular expression given as the first argument to find places in the second argument and divides the second argument at those places.
stat
Tells you everything you never wanted to know about a file. Like this: ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, $ctime,$blksize,$blocks) = stat $file;
substr
Returns part of the string first argument. It starts at the character position in the second argment and ends at the end of the string or the length specified in the third argment.
system
Executes the given Unix command as a separate process. Be careful when using this in a CGI program.
time
Returns the current time in Unix time format.
undef
Makes scalars undefined. The defined function will return false after this function. (However, exists may still return true for hash table values that are undefined.)
unlink
Deletes the given file name.
unshift
Opposite of shift.
values
Given a hash table as an argument, it returns an array of all the values in that hash table. (Also see keys.)