“$a"究竟是什么?和“$b"在 Perl 的“sort()"中功能? [英] What exactly are "$a" and "$b" in Perl's "sort()" function?

查看:12
本文介绍了“$a"究竟是什么?和“$b"在 Perl 的“sort()"中功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何使用 Perl 的 sort() 函数获得我想要的结果,这更多是关于 sort() 内部工作原理的问题.

$a"和$b"变量从何而来?我通读了排序文档,似乎不清楚.什么是$a"和$b"?它们有何特别之处?

例如:

my @sorted_list = sort {$a cmp $b} @unsorted_list;

sort 是如何知道如何处理$a"和$b"的,为什么没有收到$a"或$b"的全局符号需要明确的包名"错误?

解决方案

$a$b 是免除的全局变量;它们是豁免的,因为 Perl 允许它们在没有声明的情况下(在任何地方)使用.它们由 sort 函数设置.在 sort(严格模式下)中使用任何其他未声明的全局都会触发错误.

sort 函数接受各种形式的输入,一种是代码块,也就是您所指的形式.

{$a cmp $b} 是一个代码块,它被解析并作为代码块"传递给 sort 函数,Perl 检查 sort 的参数,如果它收到一个代码块,sort 将设置 $a$b,如果它们作为包全局变量存在于代码块中,并将被排序的每对项目分配给 $a$b.您所要做的就是参考它们来控制排序算法.否则,使用内部算法(我认为是归并排序).

http://perldoc.perl.org/functions/sort.html

$a$b 不是词法,它们是包全局变量(或只是全局变量).

在一个主要的你可以写:

sort {$main::a cmp $main::b} @list;

或者在另一个包中,你可以这样写:

package foo;排序 {$foo::a cmp $foo::b} @list;

您实际上不应该像这样添加前缀;我正在证明 $a$b 实际上是当前包中的全局变量,而不是排序函数中的一些神奇的 $a,尽管Perl 知道即使在严格模式下也允许您定义它们.

你不能只使用任何变量(在严格模式下).试试:

sort {$A cmp $B} @list;全局符号$A"需要在 sort.pl 中显式包名

您不能在排序范围内使用词法(我的 $a).

我的$a;排序 {$a cmp $b} @list;不能在 sort.pl 第 13 行的排序比较中使用my $a".

$a$b 在 Perl 中是特殊的任何地方.它们免于严格模式,这与排序无关,尽管 sort 是豁免的原因.

I understand how to get the results I want from using Perl's sort() function, this is more a question about the inner workings of sort().

Where do the "$a" and "$b" variables come from? I read through the documentation for sort and it seems unclear. What are "$a" and "$b" and what makes them special?

ex:

my @sorted_list = sort {$a cmp $b} @unsorted_list;

How does sort know what to do with "$a" and "$b" and why don't you get the "Global symbol requires explicit package name" error for "$a" or "$b"?

解决方案

$a and $b are exempt global variables; they are exempt in that Perl allows them to be used (anywhere) without being declared. They are set by the sort function. Use of any other undeclared global in sort (in strict mode) will trigger an error.

The sort function accepts various forms of input, one being a code block, which is the form you are referring to.

{$a cmp $b} is a code block, it is parsed and passed as a "chunk of code" to the sort function, and Perl checks the arguments for sort and if it receives a code block, sort will set $a and $b, if they exist as package globals within the code block, and assign each pair of items being sorted to $a and $b. All you have to do is refer to them to control the sort algorithm. Otherwise, the internal algorithm is used (which I think is merge sort).

http://perldoc.perl.org/functions/sort.html

$a and $b are not lexicals, they are package globals (or just globals).

In a main you can write:

sort {$main::a cmp $main::b} @list;

Or in another package, you could write:

package foo;

sort {$foo::a cmp $foo::b} @list;

You shouldn't actually prefix like this; I am demonstrating that $a and $b are actually globals within your current package, and not some magic $a within the sort function, although Perl knows to allow you to define them even with strict mode.

You can't just use any variables (in strict mode). Try:

sort {$A cmp $B} @list;

Global symbol "$A" requires explicit package name at sort.pl

You cannot use a lexical (my $a) in scope of sort.

my $a;
sort {$a cmp $b} @list;

Can't use "my $a" in sort comparison at sort.pl line 13.

$a and $b are special anywhere in Perl. They are exempt from strict mode, which is unrelated to sort, though sort was the reason for the exemption.

这篇关于“$a"究竟是什么?和“$b"在 Perl 的“sort()"中功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆