In Perl, do “$a” and “$b” have any special use outside of the sort() function?

天涯浪子 提交于 2019-12-10 10:19:53

问题


I asked a question about the use of "$a" and "$b" in Perl's sort() function the other day:

What exactly are "$a" and "$b" in Perl's "sort()" function?

I now have a follow up question. Are "$a" and "$b" only used by sort() or is there any other Perl function that takes advantage of these special global variables?

Or even if no other function uses them, are there any situations outside of sort() that you would use "$a" or "$b"?

Edit:

To clarify:

In short, the question is can "$a" and "$b" be used by something other than sort()?

I am just curious to see what other situations they can be used in. I have never seen "$a" or "$b" used by anything else and was wondering have any other special use outside of sort().


回答1:


can "$a" and "$b" be used by something other than sort()?

As it has already been explained, there's nothing special about $a and $b (except that they are predeclared). Asking if an ordinary variable can be used by some thing other than one function makes no sense. Here is $a being used by something that isn't sort:

 $ perl -E'$a=123; say $a;'
 123

That said, it's not a very good idea to do so. It's best to use lexical variables, and declaring $a or $b as lexical variables will render them unavailable to use in sort (or in List::Util's functions) without an overriding our ($a,$b);.

$ perl -e'sub foo { my ($a) = @_; "..."; sort { $a cmp $b } @a; "..." }'
Can't use "my $a" in sort comparison at -e line 1.



回答2:


Other non-built-in functions, such as List::Util::reduce, use them too.

Could you explain why you are asking, though? I feel like you are looking for something bigger than that as an answer.




回答3:


$a and $b are the names the sort function assigns to the two elements it is comparing, which are the parameters it assigns to the "compare()" user-supplied callback function.

You can think using the "$a", "$b" logic in any function you design, to which you provide a function as a parameter, and provide two (or more) arguments to that function...

Hope I was clear enough...




回答4:


The short answer is "no, they're not special except in the context of sort". However, I did have occasion to use them outside of a sort routine recently, which is kind of germane to your question.

I used $a and $b directly in order to utilize my sort function for a single comparison. I scoped them with local and used a do block to contain the scope:

my $cmp = do {
    local $a = $summary->{current_version};
    local $b = $latest_version_on_usb;
    cmp_version();
};

cmp_version is a subroutine designed to be used with sort, so, normally its use looks like this:

my @sorted_versions = sort cmp_version @old_versions;

Because cmp_version knows it will be called by sort, it does all of its work on $a and $b internally.

The fact that I wanted to re-use my sort subroutine in a non-sorting context (I just wanted the output of a single "$foo cmp_version $bar" comparison as if cmp_version was a real operator like <=> or cmp ) is what prompted this. It's the only reason I can think of to use $a and $b unless it's in a sort block or subroutine--the fact that they are special in the context of sort means that messing around with them outside that context could end up biting you.



来源:https://stackoverflow.com/questions/26143243/in-perl-do-a-and-b-have-any-special-use-outside-of-the-sort-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!