Bootstrap FreeKB - Perl (Scripting) - Getting Started with Subroutines
Perl (Scripting) - Getting Started with Subroutines

Updated:   |  Perl (Scripting) articles

A subroutine is a collection of reusable markup. In this example, there is a subroutine named "foo" that will print "Hello World";

sub foo {
  print "Hello World\n";
}

 

In this example, the following markup will invoke the subroutine, which will print "Hello World".

foo();

 

Let's consider this example.

sub foo {
  $bar = "Hello World";
}

 

Since print was not used in the subroutine, we need to do this to print "Hello World".

my $foo = foo();
print "$foo\n";

 

Or, even better, let's return $bar.

sub foo {
  $bar = "Hello World";
  return $bar;
}

 

Now we can call the subroutine and print $bar.

foo();
print "$bar\n";



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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