Perl排序;干净地处理命名空间中的$ a,$ b包全局变量 [英] Perl sorting; dealing with $a, $b package globals across namespaces cleanly
问题描述
假设我有一个包含子例程的实用程序库(other
)
(sort_it
),我想使用它返回任意排序的数据.
它可能比这更复杂,但这说明了
关键概念:
Suppose I have a utility library (other
) containing a subroutine
(sort_it
) which I want to use to return arbitrarily sorted data.
It's probably more complicated than this, but this illustrates the
key concepts:
#!/usr/local/bin/perl
use strict;
package other;
sub sort_it {
my($data, $sort_function) = @_;
return([sort $sort_function @$data]);
}
现在让我们在另一个软件包中使用它.
Now let's use it in another package.
package main;
use Data::Dumper;
my($data) = [
{'animal' => 'bird', 'legs' => 2},
{'animal' => 'black widow', 'legs' => 8},
{'animal' => 'dog', 'legs' => 4},
{'animal' => 'grasshopper', 'legs' => 6},
{'animal' => 'human', 'legs' => 2},
{'animal' => 'mosquito', 'legs' => 6},
{'animal' => 'rhino', 'legs' => 4},
{'animal' => 'tarantula', 'legs' => 8},
{'animal' => 'tiger', 'legs' => 4},
],
my($sort_by_legs_then_name) = sub {
return ($a->{'legs'} <=> $b->{'legs'} ||
$a->{'animal'} cmp $b->{'animal'});
};
print Dumper(other::sort_it($data, $sort_by_legs_then_name));
由于一个细微的问题,这不起作用. $a
和$b
是软件包
全球当包裹在$main::a
和$main::b
中时,它们指的是$main::a
和$main::b
关闭.
This doesn't work, due to a subtle problem. $a
and $b
are package
globals. They refer to $main::a
and $main::b
when wrapped up in
the closure.
我们可以这样说来解决此问题:
We could fix this by saying, instead:
my($sort_by_legs_then_name) = sub {
return ($other::a->{'legs'} <=> $other::b->{'legs'} ||
$other::a->{'animal'} cmp $other::b->{'animal'});
};
这有效,但是迫使我们对实用程序包的名称进行硬编码
到处.是要更改的,我们需要记住要更改
代码,而不仅仅是可能会使用的use other qw(sort_it);
语句
出现在现实世界中.
This works, but forces us to hardcode the name of our utility package
everywhere. Were that to change, we'd need to remember to change the
code, not just the use other qw(sort_it);
statement that would likely
be present in the real world.
您可能会立即考虑尝试使用__PACKAGE__
.刮风
向上评估为主要". eval("__PACKAGE__");
也是如此.
You might immediately think to try using __PACKAGE__
. That winds
up evaluating to "main". So does eval("__PACKAGE__");
.
使用 caller
的技巧可以起作用:
There's a trick using caller
that works:
my($sort_by_legs_then_name) = sub {
my($context) = [caller(0)]->[0];
my($a) = eval("\$$context" . "::a");
my($b) = eval("\$$context" . "::b");
return ($a->{'legs'} <=> $b->{'legs'} ||
$a->{'animal'} cmp $b->{'animal'});
};
但是这很黑魔术.似乎应该有 一些更好的解决方案.但我还没有找到它或想通它 还没出来.
But this is rather black-magical. It seems like there ought to be some better solution to this. But I haven't found it or figured it out yet.
推荐答案
Use the prototype (solution originally proposed in Usenet posting by ysth).
在Perl上工作> = 5.10.1(不确定之前的版本).
Works on Perl >= 5.10.1 (not sure about earlier).
my($sort_by_legs_then_name) = sub ($$) {
my ($a1,$b1) = @_;
return ( $a1->{'legs'} <=> $b1->{'legs'} ||
$a1->{'animal'} cmp $b1->{'animal'});
};
我得到的结果是:
$VAR1 = [
{
'legs' => 2,
'animal' => 'bird'
},
{
'legs' => 2,
'animal' => 'human'
},
{
'legs' => 4,
'animal' => 'dog'
},
{
'legs' => 4,
'animal' => 'rhino'
},
{
'legs' => 4,
'animal' => 'tiger'
},
{
'legs' => 6,
'animal' => 'grasshopper'
},
{
'legs' => 6,
'animal' => 'mosquito'
},
{
'legs' => 8,
'animal' => 'black widow'
},
{
'legs' => 8,
'animal' => 'tarantula'
}
];
这篇关于Perl排序;干净地处理命名空间中的$ a,$ b包全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!