Perlaccess : Tutorials : Subroutines
Calling Subroutines (5.1)
by Jacob A. Wheeler
http://www.bigresources.com

Subroutines will make your life so much easier once you start implementing them into your code. They allow you to not only seperate different functions from others, but they allow you to call any of the functions whenever you want. Which means it doesn't matter what order all your code is in. You can call a subroutine that's located at the end of your script if you want it to be the first thing executed!

To get you familiar with how subroutines look, I have created some sample code below:

  1. #!/usr/bin/perl
  2. print "Content-type: text/html\n\n";
  3. &getIP;
  4. &display;

  5. #Sub GetIP will store your IP in $IP
  6. sub GetIP {
  7. $IP = $ENV{'REMOTE_ADDR'};
  8. }

  9. #Sub display will now output your IP
  10. sub display {
  11. print "Your IP Address: $IP\n";
  12. }

Checkout Feeding Subroutines to learn howto pass variables to a subroutine from a subroutine call.