Perl is used to create the equivalent of DOS batch files or C shell scripts.It is used for a variety of tasks like Web development and most commnly it is used to develop CGI scripts. One of the nice things about Perl is that, because it is a scripting language, people give away source code for their programs. This gives you the opportunity to learn Perl by example, and you can also download and modify thousands of Perl scripts for your own use. One of the bad things about Perl is that much of this free code is impossible to understand. You create a subroutine with the word sub.
All variables passed to the subroutine arrive in an array called _. Therefore, the following code works
show ('cat', 'dog', 'eel');
sub show { for ($i = 0; $i <= $#_; $i++) { print $_[$i], "\n"; } }
Remember that $# returns the highest index in the array (the number of elements minus 1), so $#_ is the number of parameters minus 1. If you like that sort of obtuseness, then you will love PERL.
You can declare local variables in a subroutine with the word local, as in:
sub xxx { local ($a, $b, $c) ... }
You can also call a function using &, as in:
&show ('a', 'b', 'c');
The & symbol is required only when there is ambiguity, but some programmers use it all the time.
To return a value from a subroutine, use the keyword return.
|
| Author: Redhat 12 Jun 2008 | Member Level: Silver Points : 1 |
This is very good information,Continue posting such useful articles.
|