在Perl中,如何将函数作为另一个函数的参数传递?

时间:2012-05-09 20:23:35

标签: perl oop function argument-passing

我写了以下Perl类:

package Menu;
use strict;

my @MENU_ITEMS;
my $HEADER = "Pick one of the options below\n";
my $INPUT_REQUEST = "Type your selection: ";

sub new {
    my $self = {};
   $self->{ITEM}   = undef;
   $self->{HEADER} = undef;
   $self->{INPUT_REQUEST} = undef;
   bless($self);
   return $self;
}

sub setHeader {
    my $self = shift;
    if(@_) { $self->{HEADER} = shift }
    $HEADER = $self->{HEADER}."\n";
}

sub setInputRequest {
    my $self = shift;
    if(@_) { $self->{INPUT_REQUEST} = shift }
    $INPUT_REQUEST = $self->{INPUT_REQUEST}." ";
}

sub addItem {
    my $self = shift;
    if(@_) { $self->{ITEM} = shift }
    push(@MENU_ITEMS, $self->{ITEM}); 
    }

sub getMenu {
    my $formatted_menu .= $HEADER;
    my $it=1;
    foreach(@MENU_ITEMS) {
        $formatted_menu.=$it.". ".$_."\n";
        $it++
      }
    $formatted_menu.=$INPUT_REQUEST;
    return $formatted_menu;
}
1;

如果我调用以下perl脚本:

#!/usr/bin/perl
use strict;
use Menu;

my $menu = Menu->new();
$menu->addItem("First Option");
$menu->addItem("Second Option");
print $menu->getMenu;

我将得到以下输出:

Pick one of the options below
1. First Option
2. Second Option
Type your selection:

我想以一种方式修改给定的类,我可以将第二个参数传递给方法addItem()

像这样的东西: $menu->addItem("First Option", &firstOptionFunction());

当且仅当选择 First Option时,才会执行$firstOptionFunction

有没有办法在Perl中实现这种行为? 谢谢!

1 个答案:

答案 0 :(得分:7)

您希望pass a reference到子程序。

$menu->addItem("First Option", \&firstOptionFunction);

你的addItem方法可能如下所示:

sub addItem { ## your logic may vary
    my ( $self, $option, $code ) = @_;
    if ( $option eq 'First Option' ) {
        $code->();
    }

    $self->{ITEM} = $option;
    push @MENU_ITEMS, $option;

    return;
}

正如您在评论中提到的,您可能希望不将子例程作为参考传递,而是将其存储在其他位置。这样的事情可能有用:

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    $self->{f_o_code} = \&firstOptionFunction; ## use a better name than f_o_code
    return $self;
}

## add your other methods

sub addItem { ## your logic may vary
    my ( $self, $option ) = @_;
    if ( $option eq 'First Option' ) {
        $self->{f_o_code}->();
    }

    $self->{ITEM} = $option;
    push @MENU_ITEMS, $option;

    return;
} ## call like $menu->addItem( 'First Option' );