Perl 使用 coderef 作为子例程的参数

发布于 2024-12-19 05:44:48 字数 265 浏览 2 评论 0原文

我有以下子例程:

sub my_sub {
    my $coderef = shift;    
    $coderef->();
}


sub coderef {
    my $a = shift;
    my $b = shift;

    print $a+$b;
}

并且想要以这种方式调用 my_sub(\coderef($a,$b)) 即我想提供代码引用的参数并在my_sub 函数。在perl中可以做这样的事情吗?

I have the following subroutines:

sub my_sub {
    my $coderef = shift;    
    $coderef->();
}


sub coderef {
    my $a = shift;
    my $b = shift;

    print $a+$b;
}

and want to call my_sub(\coderef($a,$b)) in this manner i.e I want to provide the arguments of the code ref with it and run it on the my_sub function. Is it possible to do something like this in perl?

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

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

发布评论

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

评论(5

哑剧 2024-12-26 05:44:48

如果从表面上看这些潜艇,那么 my_sub 不会做任何事情。

这里发生了两件事:

  1. 定义 coderef

    my $adder = sub { my ( $first, $second ) = @_; $第一 + $第二 };
    
    # 添加前两个参数
    
  2. 使用必要的参数执行它

    print $adder->(2,3); #'5'
    

假设 my_sub 是某种函子,并将 coderef 作为其第一个参数传递:

sub functor {
    my $coderef = shift;  # Pull of first argument
    $coderef->( @_ );     # Rest of @_ are coderef arguments
                          # Or simply : sub functor { +shift->( @_ ) }
}

# Usage:

print functor ( $adder, 2, 3 );  # '5'

If those subs are to be taken at face value, my_sub isn't doing anything.

There are two things going on here:

  1. Define the coderef

    my $adder = sub { my ( $first, $second ) = @_; $first + $second };
    
    # Adds first two arguments
    
  2. Execute it with the necessary parameters

    print $adder->(2,3);  # '5'
    

Assuming my_sub is some kind of a functor that is passed the coderef as its first argument:

sub functor {
    my $coderef = shift;  # Pull of first argument
    $coderef->( @_ );     # Rest of @_ are coderef arguments
                          # Or simply : sub functor { +shift->( @_ ) }
}

# Usage:

print functor ( $adder, 2, 3 );  # '5'
魂ガ小子 2024-12-26 05:44:48

如果我正确理解您的问题,您需要将对 coderef 子例程的调用包装在另一个匿名子例程中,如下所示:

my_sub(sub { coderef(2, 3); }); # replace 2, 3 with whatever arguments 

If I understand your question correctly, you need to wrap the call to your coderef subroutine in another anonymous subroutine, like so:

my_sub(sub { coderef(2, 3); }); # replace 2, 3 with whatever arguments 
你是年少的欢喜 2024-12-26 05:44:48

这是你想要的吗?

use warnings;
use strict;

&my_sub( \&coderef );

sub my_sub {
    my $coderef = shift;
    $coderef->(2, 3);
}

sub coderef {
    my $a= shift;
    my $b = shift;

    print $a+$b;
}

Is this what you want?

use warnings;
use strict;

&my_sub( \&coderef );

sub my_sub {
    my $coderef = shift;
    $coderef->(2, 3);
}

sub coderef {
    my $a= shift;
    my $b = shift;

    print $a+$b;
}
无人接听 2024-12-26 05:44:48

使用匿名子例程。

my $coderef = sub {
    my ($aa, $bb) = @_;
    print $aa + $bb;
};

sub my_sub {
    my ($c_ref, @params) = @_;
    $c_ref->(@params);
}

my_sub($coderef, 2, 3);

Use an anonymous subroutine.

my $coderef = sub {
    my ($aa, $bb) = @_;
    print $aa + $bb;
};

sub my_sub {
    my ($c_ref, @params) = @_;
    $c_ref->(@params);
}

my_sub($coderef, 2, 3);
小嗷兮 2024-12-26 05:44:48

另一个想法。也许闭包可以解决您的问题?如果您将 coderef 编写为工厂,那么您可以像这样编写代码:

use strict;
use warnings;

my_sub(coderef(2,3));

sub my_sub {
  my $coderef = shift;    
  $coderef->();
}

sub coderef {
    my $a = shift;
    my $b = shift;

    return sub { print $a + $b };
}

OUTPUT

5

Another idea. Perhaps a closure solves your problem? If you write coderef as a factory then you can code like this:

use strict;
use warnings;

my_sub(coderef(2,3));

sub my_sub {
  my $coderef = shift;    
  $coderef->();
}

sub coderef {
    my $a = shift;
    my $b = shift;

    return sub { print $a + $b };
}

OUTPUT

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