How To Wiki

How to use subroutines in PERL

1,797pages on
this wiki
Add New Page
Talk0 Share

In PERL subroutines (sub) are like functions they can be called to preform repetitive tasks.

Calling SubroutinesEdit

  • Call a subroutines
    1. &subFunction();
  • Call a sub to return a value

         1. $out = &subFunction();


        2. $out = subFunction();

  • Call a sub in a if statement
    1. if ( &subFunction() ) {}
      1. have the sub return a 0 for false or 1 for true
  • Pass a variable into a sub
    1. &subFunction($var1);
    2. &subFunction($var1, $var2);

Subroutines StructureEdit

  • Basic
sub subsName {
   print "hello\n";
  • Passing vars into a sub
sub subsName {
    my ($a, $b) = ($_[0], $_[1]);
    print "$a is the first var passed in, and $b is the second.\n";
  • Returning Values
    1. Values are returned by being the last operation in the sub
sub subsName {
   my $a = $_[0];
   $b = $a + 1;
  • Multiply return options
sub subsName {
   my $a = $_[0];
   if($a > 12){
      return 1;
   } else {
   return 0;

From HowTo Wiki, a Wikia wiki.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.