
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Define and Call a Subroutine in Perl
The general form of a subroutine definition in Perl programming language is as follows −
sub subroutine_name { body of the subroutine }
The typical way of calling that Perl subroutine is as follows −
subroutine_name( list of arguments );
In versions of Perl before 5.0, the syntax for calling subroutines was slightly different as shown below. This still works in the newest versions of Perl, but it is not recommended since it bypasses the subroutine prototypes.
&subroutine_name( list of arguments );
Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before executing it, it doesn't matter where you declare your subroutine.
Example
#!/usr/bin/perl # Function definition sub Hello { print "Hello, World!\n"; } # Function call Hello();
Output
When the above program is executed, it produces the following result −
Hello, World!
Advertisements