A Perl 6 Request: No Duff's Device

jjohn on 2002-04-26T12:36:26

Duff's device is a ingenious and nails-on-chalkboard irrating C "idiom" that was designed to move bytes serially into some hardware device (not memory, probably a video card(?)). It exists because a simpler while loop took too long. It is an example of loop unrolling, a technique somewhat unused in Perl because it is only justifiably employed in high performance applications (eg. animation, games). What makes this code so abhorrant is that it looks like the result of a high-speed impacted between a switch statement and a while loop.

Please, please, great Larry and Damian, do not let Perl 6 allow insanity like this. Perhaps this is the reason Perl has no switch statement?


Blimey

Dom2 on 2002-04-26T13:14:02

I just checked tha apocalypse and it is actually allowed!!! I was most surprised. Although you do have to go slightly out of your way to use it, the usual:

    given $foo {
        when "first" { ... }
    }

Syntax won't allow it (when appears to imply a terminal break), you have to do:

    given $foo {
        if /^first$/ { ... }
    }

Now there is no implied break. Damned weird...

Just goes to show ...

jhi on 2002-04-26T17:22:57

... what pumpkining does to one's brain: I find Duff's device to be completely fine C.

Re:Just goes to show ...

jmm on 2002-04-29T14:51:21

On the other hand, it seems rather pointless to use it in Perl. If you need a tight loop of just a few instructions, why are you running with an interpreted language? If you don't need that tight loop, write the code clearly.

Re:Just goes to show ...

jhi on 2002-04-30T16:52:23

But, but, Duff's device *is* clear C...

Re:Just goes to show ...

jmm on 2002-04-30T17:40:45

Um, well, ...

It works well, and I understand it fine, and I like it, but there is an inherent inclarity in having the targets of the switch nested an extra block level below the switch body, inside a different control construct. Imagine putting an else block inside a structure nested within the then block - that's the closest you can come to doing the same thing anywhere else in the language without using goto's.

Tom Duff (whom I used to play Dungeons and Dragons with before he moved from Toronto to California to go to Industrial Light and Magic) expressed his own reservations when he posted the original description. He was surprised when it worked and uncertain whether that was good or bad.

Re:Just goes to show ...

jjohn on 2002-05-03T12:13:32

for some values of 'clear', sure. ;-)