In Perl, what if "$ a" and "$ b" have special uses outside of the sort () function?

I asked a question about using "$ a" and "$ b" in a Perl function sort()

the other day:

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

Now I have the next question. sort()

Are "$ A" and "$ b" used only, or is there any other Perl function that takes advantage of these special globals?

Or, even if no other function uses them, are there any situations outside sort()

that you would use "$ a" or "$ b"?

Edit:

To clarify:

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

?

I'm just curious to know in what other situations they can be used. I have never seen "$ a" or "$ b" used by anything else and wondered if there is any other special use out of it sort()

.

+3


source to share


4 answers


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

?

As already explained, there is nothing special about $a

and $b

(except that they are predefined). The question of whether an ordinary variable can be used by some feature other than a single function does not make sense. What is $a

used here is what is not sort

:

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

      



However, this is not a good idea. Better to use lexical variables and declare $a

or $b

, since lexical variables will make them unavailable for use in sort

(or in List :: Util functions) without 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.

      

+6


source


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



Could you explain why you are asking? I feel like you are looking for something more than this as an answer.

+2


source


$ a and $ b are the names that the sort function assigns to the two elements it compares, which are parameters that it assigns to the comparison function (), which is supplied by the user (

).

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

I hope I was clear enough ...

+1


source


The short answer is "no, they are not special except in context sort

." However, I had to sometimes use them outside of the procedure sort

, which is native to your question.

I used $a

and $b

directly to use my function sort

for one comparison. I attached them to local

and used a block do

to hide the scope:

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

      

cmp_version

is a subroutine intended to be used with sort

, so generally its usage is as follows:

my @sorted_versions = sort cmp_version @old_versions;

      

Because he cmp_version

knows that he will be called sort

, he does all his work internally $a

and $b

internally.

The fact that I wanted to reuse the sort routine in a non-sorting context (I just want the result of a single comparison " $foo cmp_version $bar

" as if it cmp_version

were a real type operator <=>

or cmp

) is what prompted this. This is the only reason I can use $a

it $b

, and if it's not in a block or subroutine sort

- the fact that they are special in context sort

means that messing with them outside of that context can end up biting you.

+1


source







All Articles