English 中文(简体)
你们如何在Perl职能中找到多重论点?
原标题:How do you get multiple arguments in Perl functions?
  • 时间:2012-01-12 07:02:39
  •  标签:
  • perl
  • shift

在我的法典中,我一直在使用一种功能呼吁的相当原始的提取参数:

sub addSix ($$$$$$) {
    my ($a, $b, $c, $d, $e, $f) = (shift, shift, shift, shift, shift, shift);
    return $a + $b + $c + $d + $e + $f;
}

print addSix (1, 2, 3, 4, 5, 6) . "
";

(针对原始编码,斜线为多条<条码>中途/代码>电话)。

现在,我和我似乎很想,但Perl可能有这样的东西:

my ($a, $b, $c, $d, $e, $f) = shift (6);

或类似情况。

但我找不到这样的东西。 我知道,我可以为此使用阵列,但我认为我仍然不得不把阵列打成单体。 就上述例子而言,这不会太坏,因为6个参数相似,但我更关心这些参数实际上不适合作为阵列的情况。

如何在不以<条码>临时关键词为最终内容的情况下提取参数?

最佳回答

你可以简单地说:

my ($a, $b, $c, $d, $e, $f) = (@_);

If you didn t have that prototype, and if that sub got called with more than six arguments, the ones after the sixth are simply "not matched", $f would be set to the sixth argument.

如果你想在第六点之后抓住所有论点,你可以这样做。

my ($a, $b, $c, $d, $e, $f, @others) = (@_);

If your list of scalars is longer than the list on the right side, the last elements will be undef.

问题回答

除非有real需要,否则对原型的使用会受到很大抑制。

与Perl一样,这样做的方法不止一个。

这里只能保证增加前六项参数:

use List::Util  sum ;

sub addSix { sum @_[0..5] }

或者,如果你喜欢自律守则:

sub addSix {

    my @firstSix = @_[0..5];  # Copy first six elements of @_
    return sum @firstSix;
}

我认识到这一点是老的,但我想到的是改变多种价值观的更好办法。 这只是一旁之旁......。 主要用于教育目的。

Sure, ($x,$y) = @_,如果你想要保留@_,那么你可能想以某种理由改变你的论据吗? 或许,你希望根据<代码>@中其余论点的数量确定任何额外的替代功能。

我认为,这样做的最干净的一条线方式是绘制一个简单的地图。

sub shiftySub {
    map { $_ = shift } my ($v, $w, $x, $y);
    # @_ now has up to 4 items removed
    if (@_) { ... } # do stuff if arguments remain
}
  • If 4 arguments are provided, @_ is now empty in the sub scope.
  • If 5 arguments are provided, @_ has 1 item remaining in the sub scope.
  • If 3 arguments are provided, @_ is empty, and $y is undef in the sub scope.

关于https://stackoverflow.com/users/14860/paxdiablo>paxdiablo 理论上的<代码>第(6)条操作者,我们可以建立我们自己的职能,开展这项行动。

sub shifter (@;$) {
    my ( $array, $n ) = ( @_, 1 );
    splice( @$array, 0, $n );
}

该职能通过实施一个双向原型(这是你本应使用原型的有限原因之一),确保阵列在电话范围上转移。 之后,你只好像这样做了。

my @items = ( one ,  two ,  three ,  four );
my ($x, $y) = shifter(@items, 2);
# or as a replacement for shift
my $z = shifter(@items)
# @items has 1 item remaining in this scope!

当然,你也可以利用<代码>中继器/代码”在您的其他分行内的功能。 如同这种职能一样,我们的主要工作是,你必须跟踪操作员双方的任务数目。

我希望 有趣;<>





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签