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

The following makes me angry...
@a = (3,1,2,5,6); print join(",",sort { $a <=> $b} @a); print join(",",sort { $a <= $b} @a);

Replies are listed 'Best First'.
Re: Simple typo in numerical sort, major error
by ikegami (Patriarch) on Dec 30, 2009 at 23:12 UTC
    print join(",",sort { $a => $b} @a);
    produces an even weirder order. You'll also love
    if ($x = $y) { ... }

      Less common, but always amusing:

      >perl -wMstrict -le "my ($x, $y) = (0, 0); if ($x =! $y) { print 'Huh?!?' } if ($x) { print 'What the...' } " Huh?!? What the...

      I almost lost my mind one night over a C code mal-expression like that. Now I always set the compiler to exclude or warn about assignment in conditionals if possible, or fall back on the
          #define is   ==
          #define isnt !=
      hack if not, or both.

Re: Simple typo in numerical sort, major error
by desemondo (Hermit) on Dec 30, 2009 at 23:20 UTC
    really? maybe you should get out more...

    it is a bit unfortunate that that particular typo still produces valid code... maybe someone will be able to add a catcher for this to the warnings pragma, but I bet theres heaps more permutations than the one you found and that ikegami mentioned.
      open my $fh, ">", "important-file" or die;
      Very costly if the > was a mistyped <.

      IMO, the warnings pragma is the wrong type of tool for detecting typos like the OP made. After all, such a check only needs to be done once (or once after each change). There's absolutely no point in paying the penalty for each and every run. Check like this (if once could actually make a check that doesn't trigger too many false positives) belong in a linter. Perl::Critic is a well used linter in the Perl world. And of course one has their test suite, which should be able to catch mistakes like that.

Re: Simple typo in numerical sort, major error
by apl (Monsignor) on Dec 31, 2009 at 13:03 UTC
    Yeah, I hate it when Perl processes what I tell it rather than what I meant as well...

    When is the Development::Telepathy suite of modules getting released to CPAN?

Re: Simple typo in numerical sort, major error
by Anonymous Monk on Dec 30, 2009 at 23:16 UTC
    print join(",",sort { $a <= $b} @a); # 6,5,3,2,1 ## Almost believable print join(",",sort { $a > $b} @a); # 1,3,2,5,6 ### WTF??