How to create a subroutine in a Perl

Home > Search > How-to
  by

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.

myText();

 



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 in the box below so that we can be sure you are a human.




Comments