Subroutines
Defining Subroutines
- You can reuse a segment of Perl code by placing it within a subroutine
- The subroutine is defined using the sub keyword and a name
- The subroutine body is defined by placing code statements within the
{ } code block symbols
sub MySubroutine
{
# Your Perl code goes here.
}
- You can call a subroutine with &
prefixed to the routine name.
&MySubroutine();
- Subroutine definitions never specify formal parameters,
although parameters can be passed to the subroutine by the caller. For
example,
MySubroutine
can be called in many ways:
&MySubroutine();
&MySubroutine(
$a , $b);