11 The Perl Scripting Language in Objective-C Generate Code 128B in Objective-C 11 The Perl Scripting Language

516 11 The Perl Scripting Language generate, create none none with none projects Windows Forms Keeping var none for none iables local to a subroutine is important in many cases. The subroutine in the next example changes the values of variables and insulates the calling program from these changes by declaring and using lexical variables. This setup is more typical.

. When you pa ss values in a call to a subroutine, Perl makes those values available in the array named @_ in the subroutine. Although @_ is local to the subroutine, its elements are aliases for the parameters the subroutine was called with. Changing a value in the @_ array changes the value of the underlying variable, which may not be what you want.

The next program avoids this pitfall by assigning the values passed to the subroutine to lexical variables. The program calls the addplusone() subroutine with two variables as arguments and assigns the value returned by the subroutine to a variable.

The first statement in the subroutine declares two lexical variables and assigns to them the values from the @_ array. The my function declares these variables to be lexical. (See the tip on lexical and package variables on page 494.

) Although you can use my without assigning values to the declared variables, the syntax in the example is more commonly used. The next two statements increment the lexical variables $lcl_one and $lcl_two. The print statement displays the value of $lcl_one within the subroutine.

The return statement returns the sum of the two incremented, lexical variables.. $ cat subro none for none $one = 1; $two = 2; $ans = addplusone($one, $two); print "Answer is $ans\n"; print "Value of "lcl_one" in main: $lcl_one\n"; print "Value of "one" in main: $one\n"; sub addplusone { my ($lcl_one, $lcl_two) = @_; $lcl_one++; $lcl_two++; print "Value of "lcl_one" in sub: $lcl_one\n"; return ($lcl_one + $lcl_two) } $ perl Value of "lcl_one" in sub: 2 Answer is 5 Value of "lcl_one" in main: Value of "one" in main: 1.

After displ aying the result returned by the subroutine, the print statements in the main program demonstrate that $lcl_one is not defined in the main program (it is local to the subroutine) and that the value of $one has not changed. The next example illustrates another way to work with parameters passed to a subroutine. This subroutine does not use variables other than the @_ array it was passed and does not change the values of any elements of that array.

. Regular Expressions $ cat subro $one = 1; $two = 2; $ans = addplusone($one, $two); print "Answer is $ans\n"; sub addplusone { return ($_[0] + $_[1] + 2); } $ perl Answer is 5.

The final e none none xample in this section presents a more typical Perl subroutine. The subroutine max() can be called with any number of numeric arguments and returns the value of the largest argument. It uses the shift function to assign to $biggest the value of the first argument the subroutine was called with and to shift the rest of the arguments.

After using shift, argument number 2 becomes argument number 1 (8), argument 3 becomes argument 2 (64), and argument 4 becomes argument 3 (2). Next, foreach loops over the remaining arguments (@_). Each time through the foreach block, Perl assigns to $_ the value of each of the arguments, in order.

The $biggest variable is assigned the value of $_ if $_ is bigger than $biggest. When max() finishes going through its arguments, $biggest holds the maximum value, which max() returns..

$ cat subro $ans = max (16, 8, 64, 2); print "Maximum value is $ans\n"; sub max { my $biggest = shift; # Assign first and shift the rest of the arguments to max() foreach (@_) { # Loop through remaining arguments $biggest = $_ if $_ > $biggest; } return ($biggest); } $ perl Maximum value is 64.

Copyright © . All rights reserved.