codescracker


perl

Perl Subroutines



« Previous Tutorial Next Tutorial »


Subroutine or function in perl, is basically a group of statement(s) that is used to perform some particular task. You can also called subroutine as function or method.

Define a Subroutine in Perl

Here is the general form to define a subroutine in perl:

sub subroutine_name
{
	subroutine body
}

Call a Subroutine in Perl

Here is the general form to call a subroutine in perl:

subroutine_name(argument_list);

Important - For perl version before 5.0, the general form to call a subroutine in perl was slightly different like this:

&subroutine_name(argument_list);

The above general form to call a subroutine in perl, is still works in the newer versions of perl, but it is not recommended, because it bypass subroutine prototypes. So use the above (first) one.

Perl Subroutine Example

Here is an example program, illustrates the concept and use of subroutine in perl:

#!/usr/bin/perl

# below is function definition
sub Print_hello
{
   print("Hello, Perl\n");
}

# below is function call
Print_hello();

When the above code is executed, it will produce the following result:

Hello, Perl

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test