Learning Perl: 4.4. Arguments

Previous Page
Next Page

 

4.4. Arguments

That 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.

[*] Unless there's an ampersand in front of the name for the invocation and no parentheses (or arguments) afterward, in which case the @_ array is inherited from the caller's context. That's generally a bad idea but is occasionally useful.

[] You might recognize that this is the same mechanism as used with the control variable of the foreach loop as seen in the previous chapter. In either case, the variable's value is saved and automatically restored by Perl.

Previous Page
Next Page
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值