shadowline
Comments << Subroutines >> Forms & Hashes
Do you have a piece of script that will be used over and over and over again? Instead of repeating the same code so many times, you can enter it once and call upon it when you need it. In JavaScript this would be a function. In PERL it is called a subroutine.

sub NAME{
code here;
}

The & ampersand symbol is used to call the subroutine into action. Here is a quick example :

#!/usr/bin/perl

&mime;
print "This is a test."

sub mime{
   print "Content-type: text/html\n\n";
}

OK, so that is a rather bad example, it holds the same concept as any other subroutine though. When the script sees the subroutine call &mime; it looks for the subroutine called mime, takes the coding that's in the subroutine, and inserts that coding into the call spot. So in the example, it put the content type mime into its place before the regular print statement (having the actual content type afterwards would cause an error).

So subroutine can be shortened to sub which can really be considered as substitute.

Although it seems logical to enter the subroutines BEFORE the line that calls it, you don't have to. It doesn't really matter where in your script you put the subroutine. Some people like to keep them at the end of their script or to store them in a separate file so they are "out of the way".

On average, most people use lower case letter (and underscores) for subroutine names. This isn't a rule set in stone though. When you are calling the subroutine, be sure you are using the same CaSe LeTtErInG.

Since the idea of a subroutine is to save time, you might as well use names that are short but descriptive. Having a sub called XYZ will not usually say much on what it holds.



Arguments are data passed into a subroutine. When data is transferred into a subroutine, it gets put into a special underscore array written as @_. You can access each individual element of this array as like any other common array. The array elements would be as :
$_[0]
$_[1]
and so on... Click back to the arrays page if you need to freshen up your knowledge on them.

An example to show passing a variable into a subroutine :
#!/usr/bin/perl

&mime;
&test("This is my test text.");
print "<br>The passed text should be seen above.";

sub mime{
   print "Content-type: text/html\n\n";
}

sub test {
    print "You should see the passed text below <br>";
    print "$_[0]";
}




Using the return command, you can assign a return value to transfer back to the original call tag.

#!/usr/bin/perl

&mime;
$which_browser = &find_browser;
print "You are browsing this page using $which_browser";

sub mime{
   print "Content-type: text/html\n\n";
}

sub find_browser {
   $browser = $ENV{'HTTP_USER_AGENT'};
   return $browser;
}

- The $which_browser variable will contain the value found by the subroutine &find_browser.
- The &find_browser subroutine is checking an environment value and assigning that to a scalar variable called $browser.
- The return command takes the $browser value and sets that as the resulting value for the subroutine.
- The subroutine value is returned to the original call tag and thus becomes the value entered into $which_browser.
- The result is printed.

It sounds a bit complicated, but basically its like this... the code says "Go do some calculations and let me know what the result is.".



There may be times when you are going to be re-using the same subroutine during the same script and with other scripts on other pages. To help with this situation, you can store subroutines in a separate file and call upon them when you need them.

1. Create a separate text file with the extension of lib.
2. Cut and paste the subroutines from the main script to this text file. No shebang line required. These are subs and thus do not require it or any extra permissions. They are only reading the file.
3. on the very last line, type    1;    When the subroutine is called, this value will return true and Perl will know that the external file was accessed successfully.
4. Save.

Example : If this was saved as "getit.lib"
sub mime{
   print "Content-type: text/html\n\n";
}

sub find_browser {
   $browser = $ENV{'HTTP_USER_AGENT'};
   return $browser;
}

1;

Now to call that external page, the require command is used :

#!/usr/local/bin/perl

require "getit.lib";

&mime;
$which_browser = &find_browser;
print "You are browsing this page using $which_browser";

If you have used SSI commands, this would be similar to the include feature. There are much better uses and situations for using external pages, the above was just a small example to show how the feature works. A popular example would be using a form parsing subroutine. Instead of sticking the subroutine onto the end of every script, you can save it in its own file and call it from each script in which its needed.
Comments << Subroutines >> Forms & Hashes
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow