Perl v5.18的排序是否理解词法子程序?

bri*_*foy 9 perl subroutine lexical perl5.18

这在Perl v5.22中得到修复.


Perl v5.18的词法子程序是否有排序?我今天终于有了一个用途,我有一个复杂的排序例程,它依赖于数据结构中的当前位置来查看更深的部分.

这是一个小程序:

use v5.18;
use feature qw(lexical_subs);
no warnings qw(experimental::lexical_subs);

my sub by_numbers { $a <=> $b }

my @sorted = sort by_numbers qw( 4 8 2 3 0 5 7 6 1 9 );

say "sorted: @sorted";
Run Code Online (Sandbox Code Playgroud)

显然sort对此一无所知,因为它仍然在寻找%main::命名子例程:

% perl5.18.2 test.pl
Undefined sort subroutine "main::by_numbers" called at test.pl line 7.

% perl5.20.1 test.pl
Undefined sort subroutine "main::by_numbers" called at test.pl line 7.
Run Code Online (Sandbox Code Playgroud)

我有点失望,因为这是rjbs 在perl 5中的法子例程中使用的第一个用例.


这部分并不重要,因为我查看了当前版本的测试而不是v5.18版本.

在perl源代码中查看t/op/lexsub.t ,我发现三个涉及排序的测试.它们在隔离运行时失败,并且在主要方面有所不同:在符号表中有一个定义的同名子程序(如rjbs注释,这些测试来自当前源,并且不存在于受影响的稳定版本中.):

use v5.18;
use feature qw(lexical_subs);
no warnings qw(experimental::lexical_subs);

use Test::More;

sub _cmp { $a cmp $b }
sub bar::_cmp { $b cmp $a }
{
  package bar;
  our sub _cmp;
  package main;
  is join(" ", sort _cmp split //, 'oursub'), 'u u s r o b', 'sort our_sub'
}


{
  state sub _cmp { $b cmp $a }
  is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
    'sort state_sub LIST'
}

{
  my sub _cmp { $b cmp $a }
  is join(" ", sort _cmp split //, 'lexsub'), 'x u s l e b',
    'sort my_sub LIST'
}
Run Code Online (Sandbox Code Playgroud)

sort在所有情况下都完全忽略了词法子程序(对于perls v5.18和v5.20):

not ok 1 - sort our_sub
#   Failed test 'sort our_sub'
#   at test.pl line 29.
#          got: 'b o r s u u'
#     expected: 'u u s r o b'
not ok 2 - sort state_sub LIST
#   Failed test 'sort state_sub LIST'
#   at test.pl line 35.
#          got: 'b e l s u x'
#     expected: 'x u s l e b'
not ok 3 - sort my_sub LIST
#   Failed test 'sort my_sub LIST'
#   at test.pl line 41.
#          got: 'b e l s u x'
#     expected: 'x u s l e b'
# Tests were run but no plan was declared and done_testing() was not seen.
Run Code Online (Sandbox Code Playgroud)

除了这个测试存在问题,因为它无法隔离环境,因此很难说出测试人员正在做什么,以及每个测试需要的前一个远程设置有多少.如果有的话,测试本身是轻微记录的.


回到重要的事情

我在这里错过了什么吗?这似乎从来没有奏效.那么,诀窍是,测试文件允许它通过什么?

请不要建议解决方法.这不是我要问的原因.

rjb*_*jbs 13

我想说这是在v5.17.x中运行然后坏了,但看起来每个人都错过了它,我甚至错过了验证它是否有效.所以...它没有.或者,更幸福的是,它没有. 这修复了:

commit 2872f91877d2b05fa39d7cd030f43cd2ebc6b046
Author: Father Chrysostomos <sprout@cpan.org>
Date:   Tue Sep 16 13:10:38 2014 -0700

    Make sort bareword respect lexical subs

—something I completely missed when implementing them.
Run Code Online (Sandbox Code Playgroud)

......从v5.21.4开始,这已按预期工作并得到承诺.