Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Pearls (not really) of Perl programming

by cosimo (Hermit)
on Nov 24, 2004 at 17:27 UTC ( #410185=note: print w/replies, xml ) Need Help??

in reply to Pearls (not really) of Perl programming

I've found this in our code base, though I'm proud to say that this is not my stuff... :-)
@aFieldsList = sort { $a eq 'listtype' and -1 or $b eq 'listtype' and 1 or $a eq 'listcode' and ( $b ne 'listtype' and -1 or 1 ) or $b eq 'listcode' and ( $a ne 'listtype' and 1 or -1 ) or $a eq 'funclist' and ( $b !~ /^(listtype|listcode)$/ and -1 or + 1 ) or $b eq 'funclist' and ( $a !~ /^(listtype|listcode)$/ and 1 or -1 ) } @aFieldsList;
Maybe it'll take 2 or 3 days to understand what is goin'on here... ;-)

Replies are listed 'Best First'.
Re^2: Pearls (not really) of Perl programming
by rir (Vicar) on Nov 24, 2004 at 18:41 UTC
    This is not that baroque once you grasp how the writer is approaching the sort. I looked at this quickly and knee-jerked that the logical expression is Wrong then I realized that it was in a sort block.

    The writer is using short-circuiting to exit the block with -1 or 1 values; so that listtype sorts before listcode before funclist before every thing else.

    I don't write that way. This seems to rely on luck as the block may yield "" which sort is not advertised to accept. I think it needs a catch-all  or ( $a cmp $b ).

    Be well.

      This seems to rely on luck as the block may yield "" which sort is not advertised to accept.
      That code always returns 1 or -1 AFAICT. Can you explain?

      Update: it will return false if $a or $b isn't any of the types tested for, but false isn't "", it's both 0 and "":

      $ perl -we'@_ = sort { "" } 1,2' Sort subroutine didn't return a numeric value at -e line 1. $ perl -we'@_ = sort { !1 } 1,2'
        Thanks for the correction. It was my error to be seduced by the control flow like use of logical short circuiting to the point of thinking statements instead of an expression. More succinctly: I had another braindead moment.

        Now that you've drawn my attention to the code again. I say it is that baroque. My initial justification was just for the  and 1 stuff which I still don't find so bad.

        I would still like a or $a cmp $b or a or 0 or  die "domain err". It is not readily apparent what is to happen with other/other comparisions. This seems like a significant decision point in the code (by the identifiers) and so a good place for extra clarity. I can imagine myself referring to this snippet to understand how all the subsequent parsing code is recieving data. This may be such a recurring problem in the codebase that it seems unimportant.

        What does irk is the inconsistent use of short-circuiting, the sub-clauses are unnecessary and so confusing. (I just trusted that aspect the first time around.

        If I were going to write in that form I'd write something like:

        @List = sort { $a eq "one" and -1 or $b eq "one" and 1 or $a eq "two" and -1 or $b eq "two" and 1 # no need to check if $a eq one or $a eq "thr" and -1 or $b eq "thr" and 1 or 0 # advertising the default case }
        Be well.
        Although you give examples of strange code and it does not pretend to be good, I can't resist pointing that:

        code block given to sort must behave (as POD stays.)
        Namely, once f(a,b) return false f(b,a) must return true.
        Some versions of perl dumped core when fed some misbehaving comparision sub.
        Such core dumps were fixed, but general rule remain: comparision sub must behave

        This means that sort {rand(5) <=> rand(5)} is obviously wrong, but sort {1} also not good.

        Best regards,
        Courage, the Cowardly Dog

Re^2: Pearls (not really) of Perl programming
by Mutant (Priest) on Nov 24, 2004 at 17:33 UTC
    I love when you get the opportunity to put obfu into production code :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://410185]
[LanX]: hippo: I'm telling him constantly to upgrade to postgress...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2018-03-19 09:32 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (238 votes). Check out past polls.