arunvelusamy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

#!/usr/local/bin/perl use strict; use warnings; my $a = [1, 2, 5, 7, 4]; print sort { $a <=> $b } @$a;
When i run the above code i get following error.

Can't use "my $a" in sort comparison at /home/code/perl/arrayprob.pl l +ine 5.

but when i use it as

my $ab = [1,2,5,7,4]; print sort { $a <=> $b } @$ab;
the code works. Why do i get this error when i use $a or $b?

Thanks,

ArunVelusamy

Replies are listed 'Best First'.
Re: Error Sorting Array Reference?
by davorg (Chancellor) on Apr 25, 2006 at 10:25 UTC

    Because $a and $b are special variables that are used by sort. You shouldn't really use them in your Perl code. See perldoc sort.

    When reporting errors, it's usually a good idea to include the text of your error message. Also, try adding "use diagnostics" to your code to get a better description of the error.

    Can't use "my $a" in sort comparison at ./sort line 7 (#1)
    (F) The global variables $a and $b are reserved for sort comparisons. You mentioned $a or $b in the same line as the <=> or cmp operator, and the variable had earlier been declared as a lexical variable. Either qualify the sort variable with the package name, or rename the lexical variable.
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Error Sorting Array Reference?
by GrandFather (Saint) on Apr 25, 2006 at 10:28 UTC

    Perl is getting confused about which $a you mean - the one that sort uses, or the one that you have declared with my. It is very bad practice to use $a or $b as a variable name, and you have just shown us why!

    If you change the code to:

    #!/usr/local/bin/perl use strict; use warnings; my $aa = [1, 2, 5, 7, 4]; print sort { $a <=> $b } @$aa;

    it works fine with no errors or warnings.

    The important thing that you didn't say was what the error was. I get 'Can't use "my $a" in sort comparison', which gives the game away somewhat!


    DWIM is Perl's answer to Gödel
Re: Error Sorting Array Reference?
by cdarke (Prior) on Apr 25, 2006 at 10:28 UTC
    The error message says it all. Look at perldoc -f sort (I quote):
    If you're using strict, you must not declare $a and $b as lexicals. They are package globals.
    etc..
Re: Error Sorting Array Reference?
by pKai (Priest) on Apr 25, 2006 at 11:16 UTC

    Hello arunvelusamy,

    comparing your post text with the replies (as of 11:10 UTC), I have the impression, that you altered (added to) your original post (several?) times in reactions to the commenting posts.

    While this is fine, it is also less confusing for later readers to indicate so in the post in that case, i. e. marking altered/added passages as appropriate.

    Thank you

Re: Error Sorting Array Reference?
by Tomte (Priest) on Apr 25, 2006 at 16:02 UTC

    While I really, really don't endorse it, you actually can use $a as your array-name, if you use a prototyped sub to sort (see sort):

    #!/usr/local/bin/perl use strict; use warnings; my $a = [1, 2, 5, 7, 4]; sub mySort($$) { my ($a,$b) = @_; $a <=> $b; } print "", (sort mySort @$a), "\n"; __END__ Output: 12457

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: Error Sorting Array Reference?
by mantadin (Beadle) on Apr 25, 2006 at 10:28 UTC

    Since $a is a special var in perl, you shouldn't define own vars of that name. The same applies to $b.

    $a is used by the sort function to hold the first of each pair of values to be compared ($b is the second of each pair).

    Now you should post your errors also, so that readers can see them, but i guess, perl didn't like you to dereference (@$a) the global variable $a that already existed before you declared my $a.

Re: Error Sorting Array Reference?
by wazoox (Prior) on Apr 25, 2006 at 10:51 UTC
    You're using $a, and you shouldn't, because it's a special reserved variable name that you don't predeclare. Use $a and $b only for comparisons.
Re: Error Sorting Array Reference?
by Andrew_Levenson (Hermit) on Apr 25, 2006 at 16:49 UTC
    I hope I don't get this wrong, but I believe that $a and $b are special reserved variables used for sorting.