Questions about this topic? Sign up to ask in the talk tab.

Perl/Basics/User Defined Functions

From NetSec
(Redirected from Perl getopt)
Jump to: navigation, search

A function is defined by the programmer to create re-usable code. In our example, we will make an is_integer function that returns either 1 or undef depending on whether the scalar passed is an integer or not.

sub is_integer {
    my $scalar = shift;
    return 1 if (int($scalar) == $scalar);
    return undef;
}

Usage:

 
print "This scalar is an integer.\n" if (defined is_integer($scalar)) else print "This is not an integer.\n";
 
Perl's return function can return multiple data types and variables, i.e.:
return($scalar,@array);
To use this type of function:
my ($scalar,@array) = function();