Built-in Perl Functions

Perl functions are available to you at any place in your script and they do not need any declaration to use, being installed within your system with Perl packages. You can do a search at CPAN and find an almost exhaustive presentation of all the built-in functions, grouped by categories or alphabetically. If you want to write your own function, you can use a user-defined subroutine and than call it anywhere within your Perl code.

It’s beyond the scope of this section to clear all the aspects related to functions in a programming language. In general, a function has a name and represents a block of code which we can call anywhere we need in a program source. We use the functions in order not to repeat indefinitely the same block of code using the copy and paste techniques.

A function can have 1, 2, 3 or more parameters or even none. A parameter is a value passed to a function and could be a scalar, a list, a reference or whatever. In general, in order to call a function, we write first the name of the function, followed by parentheses. The calling parameters are separated by commas and are included inside the parentheses. In Perl we can call a function either by using parentheses or without them. The calling of a function has as result a value which is returned by that respective function.

We’ll give you as example the push function which has the syntax:

$newNumber = push(@array, list);
# or
$newNumber = push @array, list;

where push is the name of the function, and @array and list are the arguments of the function. This function adds a list of values at the end of an array and returns the new number of the elements of the array in the scalar variable $newNumber.

And now an example about how you can call the push function in a Perl script:

# define an array
@array = ("white", "gray");
$nr = push @array, ("blue", "red", "yellow");
print ("Array's number of elements: $nr\n");

We used here the print function too, in order to display the $nr scalar variable which contains the new number of the @array elements returned by push function (in this case 5).

In Perl we can use a function as a term in an expression. When Perl meets the name of a function in a Perl script, it stops executing the sequential lines of code and continues with the execution of the function code. After finishing the execution of the function, Perl returns back at the point from where the function was called, and continues with the current code that follows the called function.

Besides the built-in Perl functions, there are a lot of other functions available in the myriad of Perl modules available through CPAN. In order to use these Perl functions, first you must declare through the clause "use" the module you intend to use. And last but not least you must search through the module documentation to see what functions are available and how to use them.

We’ll hence describe only the most relevant built-in Perl functions, which are used more frequently in Perl scripts. The built-in Perl functions are grouped in the following categories:

  • Functions for scalar or strings
  • Regular expressions and pattern matching
  • Numeric functions
  • Functions for real @arrays
  • Functions for list data
  • Functions for real %hashes
  • Input/output functions
  • Functions for fixed length data or record
  • Functions for filehandles, files or directories
  • Keywords related to the control flow
  • Keyword related to scoping
  • Miscellaneous functions
  • Functions for processes and process groups
  • Keywords related to Perl modules
  • Keywords related to classes
  • Low level socket functions
  • Time related functions

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!