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

Perl/Basics/User Defined Functions

From NetSec
Revision as of 03:18, 16 July 2012 by AlizaLorenzo (Talk | contribs) (Created page with "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 sca...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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();