Perl Functions
April 26, 1999
A function is like sending out for pizza ... you call a
third-party (the pizza shop), provide some parameters (your
name, your order), and in 30 minutes or less the results appear
at the door. Functions, though, work much more quickly and
don't need to be tipped.
Perl comes with many, many functions built-in. These functions
range widely in use, from manipulating numbers and strings to
working with files on disk. Some functions require extra
information to tell them how to do their job -- these are called
parameters. Some functions are used inside of larger expressions
while others can stand alone. Many functions can be any of the
above.
Consider the humble lc function. This function accepts
one parameter, a string, and returns a copy of the string in all
lowercase letters:
$str="HELLO";
$lowerStr=lc($str);
In the above, the variable $lowerStr would contain the value
"hello". Notice how the lc function is used within an
assignment operator; after all, using lc by itself...
lc($str);
...would not achieve anything since its returned value has
nowhere to go and would evaporate. Notice, though, that the
lc function does not change the content of $str at all -- it
merely returns a modified copy of $str. Some functions
act directly on the variable passed to them -- these functions
happily stand alone. For example, the chop function hacks
off the last character in a string:
$str="one,two,three,";
chop($str);
The chop call actually modifies the value of $str to "one,
two,three". Although chop can be used standalone, that
doesn't mean it doesn't return any information. In fact, if
used in an expression, chop returns the character that it
severed off the original string. Thus:
$str="one,two,three,";
$chopped=chop($str);
The above would both chop the final comma from $str and assign
that comma to $chopped. Typically there is no need to save the
hacked off part of the string, which is why chop is commonly
used without being part of a larger expression.
Functions don't necessarily have to accept their parameters
inside parentheses. You may well see examples of functions
which are called without any parentheses, such as
"chop $str". In fact, you've already seen such an
example in this article -- the print function. As a rule of
thumb, though, it is best to keep using the parentheses to
enclose parameters being passed to the function -- print is
an exception in that it is widely accepted without parentheses.
Perl lets you get away with much syntactical sloppiness, but
that doesn't mean it is a good idea.
Using functions is straightforward, and an essential part of
Perl. The good news is that Perl includes a huge number of
built-in functions ready and waiting to make programming easier,
from handling mathematic calculations to hacking up strings into
various bits and pieces to managing times and dates to writing
to and reading from data files. The bad news is that there are
simply too many functions to summarize in this article. The
extra good news is that these functions have already been
summarized and categorized, and you can find them in the
perlfunc documentation at
CPAN,
the Comprehensive Perl Archive Network.
Perl Variables: Hashes
The Perl You Need to Know
Perl Program Flow: Conditionals and Loops
|