FreeKB - Subroutine in Perl - Getting Started
Subroutine in Perl - Getting Started

Home > Search

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";


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




Comments