- Perl Basics
- Perl Home
- Perl Basic Syntax
- Perl Data Types
- Perl Variables
- Perl Scalars
- Perl Arrays
- Perl Hashes
- Perl If-Else
- Perl Loops
- Perl Operators
- Perl References
- Perl Subroutines
- Perl Formats
- Perl Date & Time
- Perl File I/O
- Perl Advanced
- Perl Directory
- Perl Regular Expressions
- Perl Special Variables
- Perl Socket Programming
- Perl Send E-mail
- Perl Object Oriented
- Perl Error Handling
- Perl CGI Programming
- Perl Database
- Perl Packages & Modules
- Perl Process Management
- Perl Test
- Perl Online Test
- Give Online Test
- All Test List
Perl Subroutines
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 »