Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Do you know where your variables are?
 
PerlMonks  

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... ;-)


Comment on Re: Pearls (not really) of Perl programming
Download Code
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 :)
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://410185]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-04-24 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (557 votes), past polls