519 521




Linux Unleashed, Third Edition:Perl





-->















Previous
Table of Contents
Next




until Statements
The until statement is very similar to the while statement. The difference is that the until statement executes its statement block only until its conditional expression becomes True, and the while statement executes its statement block while its conditional expression is True. The following program contains an example of the until statement:


$i = 0;
until ($i > 10) {
print “$i “;
$i = $i + 1;
}


This statement continues to execute and print the value of $i until $i is larger than 10. The output from this command is


0 1 2 3 4 5 6 7 8 9 10


Any until statement can also be written as a while statement. For this reason, the until statement is not used very often. The example shown for the until statement can be rewritten as a while statement, as shown by the following code:


$i = 0;
while ($i <= 10 ) {
print “$i “;
$i = $i + 1;
}


Functions
You have already seen some of the built-in Perl functions, such as print, but you have not seen how to create your own functions. Perl refers to functions that you create as subroutines. The syntax for creating a subroutine is


sub subroutine_name {
statements;
}


The subroutine_name is a name you provide and is the name you use when calling the subroutine from your Perl code. The statements in the statement block are the statements that are executed every time you make a call to your subroutine.
To familiarize yourself with the syntax for creating a function, take a look at the following function which multiplies the values that are stored in the variables $num1 and $num2 each time it is called.


sub mult {
print “The result of $num1 * $num2 is “,$num1 * $num2, “\n”;
}


To invoke this function, you must precede the name of the function with an ampersand (&). For example, the following program assigns values to the $num1 and $num2 variables and invokes the mult function.


$num1 = 5;
$num2 = 10;
&mult;


Running this program results in the following output being displayed on the screen:



The result of 5 * 10 is 50


If this were all functions could do, they would not be incredibly useful. The real power of functions doesn’t come into play until you start defining them so that they can accept arguments and return values.

Passing Arguments to Functions
Passing arguments to functions increases their usefulness a great deal. This is because you do not have to know the names of the variables used by the function if they are being passed in as arguments. If you rewrite the mult function so that it accepts arguments, you can use it to multiply any two numbers together instead of just the numbers stored in the $num1 and $num2 variables. The following is the mult function after it has been rewritten to use arguments.


sub mult2 {
print “The result of $_[0] * $_[1] is “, $_[0] * $_[1], “\n”;
}


The variables $_[0] and $_[1], which are the first and second elements of the @_ array, are used to store the values of the first two arguments passed to the function. The following line of code uses the new mult2 function to multiply two numbers together:


&mult2(14,20);


When this command is executed, the first argument passes to mult2 (14) and is put into the first element of the @_ array; the second argument (20) is put into the second element of the @_ array. The mult2 function prints the following message on the screen:


The result of 14 * 20 is 280


Using Return Values
What happens if you do not want to print the results of the mult2 function to the screen but instead want to use the result in your program? Perl handles this problem by using a return value. Every function you write in Perl has a return value. By default, the return value is equal to the result of the last statement evaluated in the function. The mult2 function returns the result of the Perl print function, which, if everything works properly, is equal to 1. You can rewrite the mult function once again so that it returns the result of the multiplication.


sub mult3 {
$_[0] * $_[1];
}


The following code uses the mult3 function to find the results of a multiplication:


$num1 = 5;
$num2 = 10;
$result = &mult3($num1 $num2);
print “The answer is $result\n”


When this program is executed, the result of the multiplication performed by the mult3 function is returned and stored in the result variable. The program then prints the result to the screen.
Perl Operators
You have seen quite a few Perl operators in the examples in this chapter. The print command that’s been used throughout this chapter is one example of a Perl operator. You also saw the Perl operators keys, values, each, and delete which are used to perform operations on associative arrays. Perl actually provides an unbelievable number of operators. To get a good idea of what operators are available or to get a complete listing of the Perl operators, refer to the Perl manual page.



Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
521 523
516 519
519 523
521,37,artykul
519,24,artykul
INDEX (519)
517 521
519 (2)
521 522
512 519

więcej podobnych podstron