Functions

A function is like an operator - and in fact some functions double as operators in certain conditions - but with the following differences:

The only real way to tell whether something is a function or an operator is to check the perlop and perlfunc manual pages and see which it appears in.

There's an introduction to functions on page 8 of the Camel, labeled 'Verbs'.

Types of arguments

Functions typically take the following kind of arguments:

SCALAR -- Any scalar variable - 42, "foo", or $a

LIST -- Any named or unnamed list (remember that a named list is an array)

ARRAY -- A named array; usually results in the array being modified

HASH -- Any named or unnamed hash

PATTERN -- A pattern to match on - we'll talk more about these later on, in Regular Expressions

FILEHANDLE -- A filehandle indicating a file that you've opened or one of the pseudo-files that is automatically opened, such as STDIN, STDOUT, and STDERR

There are other types of arguments, but you're not likely to need to deal with them in this module.

In chapter 3 of the Camel (starting on page 141) you'll see how the documentation describes what kind of arguments a function takes.

Return values

Just as a function can take arguments of various kinds, they can return various things for you to use - though they don't have to, and you don't have to use them if you don't want.

If a function returns a scalar, and we want to use it, we can say something like:

my $age = 29.75;
my $years = int($age);

and $years will be assigned the returned value of the int() function when given the argument $age - in this case, 29, since int() truncates instead of rounding.

If we just wanted to do something to a variable and didn't care what value was returned, we could just say:

my $input = <STDIN>;
chomp($input);

While we're at it, you should also know that the brackets on functions are optional if it's not likely to cause confusion. What's likely to cause confusion varies from one person to the next, but it's a pretty safe bet to use brackets as much as possible when you're starting out, and then drop them off if you see that other people are usually doing it. Seriously. You can learn a lot about Perl style by looking at other people's code, especially code found on CPAN or given as examples in Perl books, newsgroups, etc.