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

Can anyone explain what this code is supposed to do? I have been messing around with it and I can't seem to grasp how it works :(
#!/usr/bin/perl -s # |bf; bf $file; bf -e=, $|++;undef$/;$_=$e||<>;tr/-+,.[]<>//cd;for$ a(qw%]} [while($p[$p]){ -$p[$p]-- +$p[$p]++ ,$p[$p]=ord(getc) .print(chr($p[$p])) <$p-- >$p++%){s/\Q${\substr$a,0,1}/${\substr$a,1} ;/g}eval;

Replies are listed 'Best First'.
Re: explain obfu code
by dragonchild (Archbishop) on Oct 02, 2001 at 01:17 UTC
    Here're the steps I'd take:
    1. Get rid of all the comments.
    2. Run it to make sure I didn't break anything
    3. Put a newline after every single semi-colon
    4. Run it to make sure I didn't break anything
    5. Start to fix the logic, renaming variables and the like
    6. Run it after every single edit, to make sure I didn't break anything
    7. Start getting rid of filler-code.
    8. Run it after every single edit, to make sure I didn't break anything
    A few tricks to keep in mind:
    • *)=5; is a way of making a variable named $* and setting it equal to 5.
    • Order of operations is critical.
    • Delimiters don't have to be slashes.
    That should be enough to get you started.

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: explain obfu code
by maverick (Curate) on Oct 02, 2001 at 01:53 UTC
    You could also run it through Deparse to make it look a lot less ugly...that would by my first step.

    perl -MO=Deparse


    ++$|; undef $/; $_ = $e || <ARGV>; tr/+-.<>[]//cd; foreach $a (']}', '[while($p[$p]){', '-$p[$p]--', '+$p[$p]++', ',$p[$p +]=ord(getc)', '.print(chr($p[$p]))', '<$p--', '>$p++') { s/\Q${\substr($a, 0, 1);}\E/${\substr($a, 1);}\n;/g; } eval $_;
    Yick! What is this program supposed to do anyway?

    Here's my second pass at looking at this. Look at the elements in the foreach loop. The left most character of the element is what is looked for in the replacement and the rest of the element it what a match is replaced with.

    This input

    [ < - ]
    Produces this output
    while($p[$p]){ ;$p-- ;$p[$p]-- ;} ;
    Could this be a home brew macro language parser? jeffa agree's with this idea.

    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      It's an interpreter for Brainfuck.

      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Try piping this into it -
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++.>++++++++++.>-<
      There are more elegant ways of putting that, but it's brainfuck which is rarely elegant anyways.


      Note: the above spew was (as you've probably guessed) created by yet another BF script (which can be found here).

        I had never heard of BF -- I guess you do learn something new every day.

        Anyway, in the spirit of BF Golf, I shortened your code. *Grins*

        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++.---------.----- --------------------------.++++.+++++++++++++++++++++++++++++ +++++.
Re: explain obfu code
by perrin (Chancellor) on Oct 02, 2001 at 01:52 UTC
    Perltidy is often helpful when dealing with obfuscated code.