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

hi monks,

i kneel before you once again begging for your wisdom. i have this short one line obfuscated code that just prints out a name and i was wondering if someone could explain the break down for me.

thank you,
perl noob
'' =~ ( '(?{' . ( '+]).][' ^ '[/@@){' ) . '"' . ( '^@@;.|' ^ '?,,^@^' ) . ',$/})' )
Update: can someone please tell me how ( '+]).][' ^ '[/@@){' ) translates to print and how ( '^@@;.|' ^ '?,,^@^' ) translates to allen"?

Replies are listed 'Best First'.
Re: Can someone please break this obfuscated code down for me
by VSarkiss (Monsignor) on Aug 02, 2006 at 17:08 UTC

    BrowserUK has given you the best general tip above. If you're still wondering how that string turns into that program, an editor with syntax highlighting can help. Here's a breakdown if you still need it.

    If you add some white space, you'll see the line looks like:

    '' =~ ( '(?{' . ( '+]).][' ^ '[/@@){' ) . '"' . ( '^@@;.|' ^ '?,,^@^' ) . ',$/})' )
    So it's matching the empty string against some bizarre pattern. The pattern is built up from concatenating five pieces, three of which are constants:
    • '(?{'
    • '"'
    • ',$/})'

    The other two parts are wrapped in parentheses, and are the result of combining two strings with the exclusive-or operator (^, perlop for more info). Since the parts are constant, perl evaluates them at compile time. If you test it, you'll see that the first expression evaluates to print and the second to allen".

    So now you see how Deparse got that string. If you haven't seen the ?{...} construct in Perl regular expressions, take a look at perlre. It allows you to execute code inside a regular expression. In this case it executes the code print "allen",$/.

    QED. HTH. :-)

    Update
    If you want to really get a handle on how the exclusive-or operates to generate the string, it's best to look at the bits-n-bytes level. Try running this little snippet to see.

    my @s = ( '+', ']', ')', '.', ']', '[', '[', '/', '@', '@', ')', '{', ); for (my $i = 0; $i < 6; $i++) { my ($l, $r) = ($s[$i], $s[$i+6]); printf "%s %x ^ %s %x = %s %x\n", $l, ord $l, $r, ord $r, $l ^ $r, ord $l ^ ord $r; }

      Excellent explanation :) and the code is really interesting...
Re: Can someone please break this obfuscated code down for me
by BrowserUk (Pope) on Aug 02, 2006 at 16:39 UTC

    Take a look at the docs for B::Deparse

    C:\test>perl -MO=Deparse -le"''=~('(?{'.('+]).]['^'[/@@){').chr(34).('^@@;.|'^'?,,^@^').',$/})' +)" BEGIN { $/ = "\n"; $\ = "\n"; } '' =~ m[(?{print "allen",$/})]; -e syntax OK

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Can someone please break this obfuscated code down for me
by eyepopslikeamosquito (Bishop) on Aug 02, 2006 at 21:17 UTC

    This code uses a similar technique to that used by the CPAN Acme::EyeDrops module. To illustrate, running this program:

    use Acme::EyeDrops qw(sightly); print sightly({ Regex => 1, Print => 1, Compact => 1, SourceString => "allen\n" });
    produces:
    ''=~('('.'?'.'{'.('['^'+').('['^')').('`'|')').('`'|'.').('['^'/').'"' +.('`'|'!').('`'|',').('`'|',').('`'|'%').('`'|'.').('!'^'+').'"'.'}'. +')')
    which, when run through Deparse, produces:
    '' =~ /(?{print"allen\n"})/;

    For more fun, Acme::EyeDrops allows you to pour the generated code into various shapes. For example, running:

    use Acme::EyeDrops qw(sightly); print sightly({ Shape => 'rose', Regex => 1, Print => 1, Indent => 1, SourceString => "allen\n" });
    produces:
    ''=~('('.'?' .'{'.('['^"\+").( '['^')' ).+( "\`"| (')')).( "\`"| '.'). ('['^ '/'). "\"".( '`'| '!'). ("\`"| ','). ('`'| ',').( ('`')| '%'). ('`'| '.').( '!'^'+' ).'"'.'}'. ')'); $:='.'^'~';$~=('@')| '(';$^=')'^'[';$/ ='`'|('.');$,= '(' ^'}';$\='`'|'!';$: =')'^'}';$~ ='*' |('`');$^= '+'^'_'; $/='&' |'@' ;$,='['&'~' ;$\="\,"^ '|';$:=('.')^ '~';$~="\@"| '(';$^=')'^'[';$/='`'|'.';$,= '('^'}';$\= '`'|'!';$:=')'^"\}"; ($~)= '*'|'`';$^ ='+'^'_';$/=('&')| '@';$, ='['&'~'; $\= ','^'|';$: ='.'^'~' ;$~='@'|'(' ;$^= ')' ^('[');$/= '`'|'.';$,='('^'}';$\= '`'|'!';$:= ')'^'}';$~='*'|'`';$^='+'^'_';$/="\&"| '@';$,='['&'~';$\= ',' ^(( '|'));$:=('.')^ '~';$~= '@' |(( '(' )); $^= ( ')')^'['; $/= (( '`'))|'.'; $,= "\("^ '}';$\='`' |(( '!')); $:=(')')^ '}' ;($~) ="\*"| '`' ;($^) =(( '+' ))^'_' ;($/)= '&'|'@';$,= '['&'~';$\= ','^'|';$:=('.')^ '~';$~='@'| '(';$^=')'^ '[';$/=('`')| '.';$,='('^ "\}";$\= ('`')| '!';$:="\)"^ '}';$~='*'| ('`');$^= '+'^'_'; $/='&'|'@' ;$,=('[')& ('~');$\= ','^"\|"; $:=('.')^ ('~');$~= '@'|'(';$^ =')'^'[';$/ =('`')| '.';$,= '('^'}';$\ ='`'|'!';$: ="\)"^ '}';$~ ='*'|'`'; $^='+'^'_'; ($/) ='&'| '@';$,= '[' &'~';$\= ',' ^'|' ;($:) =(( '.'))^'~' ;( $~) ='@' |(( "\(")); $^ =(( ')' ))^(( '[' )) ;$/ =(( '`' ));

Re: Can someone please break this obfuscated code down for me
by ikegami (Pope) on Aug 02, 2006 at 17:30 UTC

    would it be correct to say that this is compiled or parsed code?

    No, it would be incorrect. It's "plain" old Perl source code.

Re: Can someone please break this obfuscated code down for me
by kwaping (Priest) on Aug 02, 2006 at 18:44 UTC
    Where did you get that code? I must know what demented wizard came up with that! :)

    ---
    It's all fine and dandy until someone has to look at the code.