FANDOM


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();

            or

        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;
   $b;
}
  • Multiply return options
sub subsName {
   my $a = $_[0];
   if($a > 12){
      return 1;
   } else {
   return 0;
   }
}


From HowTo Wiki, a Wikia wiki.
Community content is available under CC-BY-SA unless otherwise noted.