4.4. ArgumentsThat subroutine called larger_of_fred_or_barney would be much more useful if it didn't force you to use the global variables $fred and $barney. If you wanted to get the larger value from $wilma and $betty,you currently have to copy those into $fred and $barney before you can use larger_of_fred_or_barney. And if you had something useful in those variables,you'd have to first copy those to other variables,say $save_fred and $save_barney. Then,when you're done with the subroutine,you'd have to copy those back to $fred and $barney. Luckily,Perl has subroutine arguments. To pass an argument list to the subroutine,place the list expression,in parentheses,after the subroutine invocation like this: $n = &max(10,15); # This sub call has two parameters The list is passed to the subroutine; that is,it's made available for the subroutine to use however it needs to. Of course,you have to store this list somewhere,so Perl automatically stores the parameter list (another name for the argument list) in the special array variable named @_ for the duration of the subroutine. The subroutine can access this variable to determine the number of arguments and the value of those arguments. This means that the first subroutine parameter is stored in $_[0],the second one is stored in $_[1],and so on. Butand here's an important notethese variables have nothing to do with the $_ variable,any more than $dino[3] (an element of the @dino array) has to do with $dino (a completely distinct scalar variable). The parameter list must be stored into some array variable for the subroutine to use it,and Perl uses the array @_ for this purpose. Now,you could write the subroutine &max to look a little like the subroutine &larger_of_fred_or_barney,but instead of using $fred,you could use the first subroutine parameter ($_[0]),and instead of using $barney,you could use the second subroutine parameter ($_[1]). And so you could end up with code something like this: sub max { # Compare this to &larger_of_fred_or_barney if ($_[0] > $_[1]) { $_[0]; } else { $_[1]; } } Well,as we said,you could do that. But it's pretty ugly with all of those subscripts,and it's hard to read,write,check,and debug,too. You'll see a better way in a moment. There's another problem with this subroutine. The name &max is nice and short,but it doesn't remind us that this subroutine works properly only if called with exactly two parameters: $n = &max(10,15,27); # Oops! Excess parameters are ignored; since the subroutine never looks at $_[2],Perl doesn't care whether there's something in there or not. Insufficient parameters are also ignored; you simply get undef if you look beyond the end of the @_ array as with any other array. Later in this chapter,you'll see how to make a better &max,which works with any number of parameters. The @_ variable is private to the subroutine;[*] if there's a global value in @_,it is saved before the subroutine is invoked and restored to its previous value upon return from the subroutine.[] This means that a subroutine can pass arguments to another subroutine without fear of losing its own @_ variable. The nested subroutine invocation gets its own @_ in the same way. Even if the subroutine calls itself recursively,each invocation gets a new @_,so @_ is always the parameter list for the current subroutine invocation.
|
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。