Subroutines

Subroutines are the key to writing legible and to some extent reusable code. The core of the program should be clean, with the flow easy to follow, the tough stuff should be done in subroutines.

You can write a subroutine anywhere in your code, but I'll always group them at the end. A subroutine is declared using the keyword sub. The subroutine needs a name, and the commands will be enclosed in curly brackets.

Subroutines are called using the & prefix, so if I called my subroutine find then I call it with &find.

Subroutines typically take arguments. Perl passes the arguments as a list of scalars. It's your job to put anything you need in the subroutine into the list. Similarly all subroutines will return a list. Arguments come into the subroutine in the @_ array.

Example code.


Richard Waterman
Fri Jan 22 01:27:29 EST 1999