【问题标题】:Perl $AUTOLOAD evaluation in v5.10 says "Bareword found where operator expected"...but v5.26 works finev5.10 中的 Perl $AUTOLOAD 评估显示“在操作员预期的位置找到了 Bareword”...但是 v5.26 工作正常
【发布时间】:2022-11-16 18:34:56
【问题描述】:

我正在使用来自 @ikegami's post hereAUTOLOAD 示例。我的 RF::Component::Multi 模块的最近 CPAN testers report 说:

Bareword found where operator expected at .../RF/Component/Multi.pm line 102, near "s/^.*:://sr"
syntax error at .../RF/Component/Multi.pm line 102, near "s/^.*:://sr"

代码在下面和 GitHub 上的here

  • Perl 5.10 不喜欢什么?
  • 是否有一个要求>5.10 的 Perl 特性隐藏在这里而我遗漏了? (我的 Perl 5.26.3 正在运行)
    • 如果可以,是否可以使其更加向后兼容?如何?
    • 如果不是,我在哪里可以找到版本以便我可以正确执行use 5.xx
  • 我需要use vars '$AUTOLOAD'吗?
# Thanks @ikegami:
# https://stackoverflow.com/a/74229589/14055985
sub AUTOLOAD
{
    my $method_name = our $AUTOLOAD =~ s/^.*:://sr;

    my $method = sub {
        my $self = shift;
        return [ map { $_->$method_name(@_) } @$self ];
    };

    {
        no strict 'refs';
        *$method_name = $method;
    }

    goto &$method;
}

【问题讨论】:

    标签: perl autoload


    【解决方案1】:

    s///r 是在 5.14 中添加的。

    my $method_name = our $AUTOLOAD =~ s/^.*:://sr;
    

    可以替换为

    ( my $method_name = our $AUTOLOAD ) =~ s/^.*:://s;
    

    【讨论】:

      【解决方案2】:

      你可以使用Perl::MinimumVersion来回答这样的问题。

      $ perlver your-code.pl
      
      
         --------------------------------------
       | file   | explicit | syntax  | external |
       | -------------------------------------- |
       | minver | ~        | v5.13.2 | n/a      |
       | -------------------------------------- |
       | Minimum explicit version : ~           |
       | Minimum syntax version   : v5.13.2     |
       | Minimum version of perl  : v5.13.2     |
         --------------------------------------
      

      并且,欲了解更多详情,

       $ perlver --blame your-code.pl
      
       ------------------------------------------------------------
       File    : minver
       Line    : 3
       Char    : 40
       Rule    : _regex
       Version : 5.013002
       ------------------------------------------------------------
       s/^.*:://sr
       ------------------------------------------------------------
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多