【问题标题】:How to execute functions from packages using function reference in perl?如何使用 perl 中的函数引用从包中执行函数?
【发布时间】:2011-05-09 22:55:17
【问题描述】:

我想使用函数引用来动态执行其他包中的函数。

我一直在尝试不同的解决方案来解决这个想法,但似乎没有任何效果! 所以,我想问这个问题,在尝试这样做时,解决方案奏效了! 但我不确定这是否是正确的方法:它需要手动工作并且有点“hacky” ”。可以改进吗?

  1. 支持所需功能的包

    package Module;
    
    # $FctHash is intended to  be a Look-up table, on-reception 
    # of command.. execute following functions
    
    $FctHash ={
        'FctInitEvaluate' => \&FctInitEvaluate,
        'FctInitExecute' => \&FctInitExecute
    };
    
    sub FctInitEvaluate()
    {
        //some code for the evalute function
    }
    
    sub FctInitExecute()
    {
        //some code for the execute function
    }
    1;
    

2。 Utility Script 需要使用包 using 函数参考

    use strict;
    use warnings 'all';
    no strict 'refs';

    require Module;

    sub ExecuteCommand()
    {
      my ($InputCommand,@Arguments) =@_;
      my $SupportedCommandRefenece = $Module::FctHash;
         #verify if the command is supported before 
         #execution, check if the key is supported
         if(exists($SupportedCommandRefenece->{$InputCommand}) )
         {
           // execute the function with arguments
           $SupportedCommandRefenece->{$InputCommand}(@Arguments);
         }
      }

      # now, evaluate the inputs first and then execute the function
      &ExecuteCommand('FctInitEvaluate', 'Some input');
      &ExecuteCommand('FctInitExecute', 'Some input');
    }

但现在,这项技术似乎奏效了!不过,有没有办法改进它?

【问题讨论】:

  • Perl 不是 JavaScript。你用“#”而不是“//”来引入注释。带有“sub”声明的行上的括号并不像您认为的那样。消除它们,例如 sub ExecuteCommand {...}
  • 函数调用 (&ExecuteCommand(...)) 上的和号前缀也不会像您认为的那样。也消除它们。
  • @Dave,实际上,我修改了他的代码使其看起来 (a) 可读 (b) Perl。但现在我有疑问:也许是 JavaScript...
  • @Pavel:我查看了问题修订历史中的原始版本,不,从一开始就是 Perl-code-trying-to-use-//-cmets。你没有破坏它,它一开始就被破坏了。 :)

标签: perl module reference package


【解决方案1】:

您可以使用can。详情请见perldoc UNIVERSAL。

use strict;
use warnings;
require Module;

sub ExecuteCommand {
    my ($InputCommand, @Arguments) = @_;
    if (my $ref = Module->can($InputCommand)) {
        $ref->(@Arguments);
    }
    # ...
}

【讨论】:

    【解决方案2】:

    您已经为使用散列作为调度表构建了一个相当标准的实现。如果这是你的意图,我认为除了清理一下之外没有任何理由做更多的事情。 can 是一个不错的选择,如果您正在尝试构建一些面向对象的东西,但如果您所追求的只是一个命令查找表,那就没有必要了。

    这是一个版本,a) 可以运行 Perl(您尝试在问题版本中使用 // 标记 cmets 是语法错误;在 Perl 中,// 是 5.10 及更高版本的“定义- or" 运算符,不是注释标记)和 b) 有更多的 perlish 重音:

    Module.pm

    package Module;
    
    use strict;
    use warnings;
    use 5.010;
    
    our $function_lookup = {
        FctInitEvaluate => \&init_evaluate,
        FctInitExecute  => \&init_execute,
    };
    
    sub init_evaluate {
        say 'In init_evaluate';
    }
    
    sub init_execute {
        say 'In init_execute';
    }
    
    1;
    

    script.pl

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    require Module;
    
    execute_command('FctInitEvaluate', 'Some input');
    execute_command('FctInitExecute',  'Some input');
    
    sub execute_command {
        my ($input_command, @arguments) = @_;
        $Module::function_lookup->{$input_command}(@arguments)
          if exists($Module::function_lookup->{$input_command});
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2016-02-02
      • 1970-01-01
      相关资源
      最近更新 更多