FreeKB - How to create a subroutine or function in a Perl
How to create a subroutine or function in a Perl

Home > Search > How-to

A subroutine is the same exact thing as a function.

A subroutine is a collection of markup that can be used in our Perl script. For example, let's say you want to print a long line of text many times in your script. You can place the long line of text inside of a subroutine.

sub myText {
  print "This is a very long line of text.";
  print "This text is going to be used many times.";
  print "Placing this text over and over in our script is going to make our script look messy.";
  print "Let's be smart and put the text in a subroutine called myText.";


Since our subroutine is called "myText" in this example, the subroutine can be used simply by placing "myText();" in the perl script.



Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 0545f in the box below so that we can be sure you are a human.