如何在 Perl 中访问函数参数?

发布于 2024-11-02 04:44:14 字数 473 浏览 4 评论 0原文

在 C++ 中我会这样做:

void some_func(const char *str, ...);
some_func("hi %s u r %d", "n00b", 420);

在 PHP 中我会这样做:

function some_func()
{
    $args = func_get_args();
}
some_func($holy, $moly, $guacomole);

How do I do that in Perl?

sub wut {
    # What goes here?
}

In C++ I would do something like this:

void some_func(const char *str, ...);
some_func("hi %s u r %d", "n00b", 420);

In PHP I would do like this:

function some_func()
{
    $args = func_get_args();
}
some_func($holy, $moly, $guacomole);

How do I do that in Perl?

sub wut {
    # What goes here?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2024-11-09 04:44:14

您会这​​样做:

sub wut {
  my @args = @_;
  ...
}

当您调用函数时,Perl 会自动填充特殊的 @_ 变量。您可以通过多种方式访问​​它:

  • 直接使用 @_ 或其中的单个元素作为 $_[0]$_[1]< /code>,等等,
  • 通过将其分配给另一个数组,如上所示
  • 通过将其分配给标量列表(或可能是散列,或另一个数组,或其组合):

    子吴特{
      我的($arg1,$arg2,$arg3,@others)=@_;
      ...
    }

请注意,在这种形式中,您需要将数组 @others 放在end,因为如果你把它放在前面,它会吞掉 @_ 的所有元素。换句话说,这不起作用:

sub wut {
  my ( $arg1, @others, $arg2 ) = @_;
  ...
}

您还可以使用 shift@_ 中提取值:

sub wut {
  my $arg1 = shift;
  my $arg2 = shift;
  my @others = @_;
  ...
}

请注意,shift 将自动起作用如果您不为其提供参数,则在 @_ 上。

编辑:您还可以通过使用哈希或哈希引用来使用命名参数。例如,如果您像这样调用 wut()

wut($arg1, { option1 => 'hello', option2 => 'goodbye' });

...您可以执行以下操作:

sub wut {
  my $arg1 = shift;
  my $opts = shift;
  my $option1 = $opts->{option1} || "default";
  my $option2 = $opts->{option2} || "default2";
  ...
}

这将是将命名参数引入到函数中的好方法,以便您可以稍后添加参数并您不必担心它们通过的顺序。

You would do:

sub wut {
  my @args = @_;
  ...
}

Perl automatically populates the special @_ variable when you call a function. You can access it in multiple ways:

  • directly, by simply using @_ or individual elements within it as $_[0], $_[1], and so on
  • by assigning it to another array, as shown above
  • by assigning it to a list of scalars (or possibly a hash, or another array, or combinations thereof):

    sub wut {
      my ( $arg1, $arg2, $arg3, @others ) = @_;
      ...
    }

Note that in this form you need to put the array @others at the end, because if you put it in earlier, it'll slurp up all of the elements of @_. In other words, this won't work:

sub wut {
  my ( $arg1, @others, $arg2 ) = @_;
  ...
}

You can also use shift to pull values off of @_:

sub wut {
  my $arg1 = shift;
  my $arg2 = shift;
  my @others = @_;
  ...
}

Note that shift will automatically work on @_ if you don't supply it with an argument.

Edit: You can also use named arguments by using a hash or a hash reference. For example, if you called wut() like:

wut($arg1, { option1 => 'hello', option2 => 'goodbye' });

...you could then do something like:

sub wut {
  my $arg1 = shift;
  my $opts = shift;
  my $option1 = $opts->{option1} || "default";
  my $option2 = $opts->{option2} || "default2";
  ...
}

This would be a good way to introduce named parameters into your functions, so that you can add parameters later and you don't have to worry about the order in which they're passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文