How does this Perl code work?
Asked Answered
K

1

9

I found this Perl program:

''=~('(?{'.(']])@+}'^'-/@._]').'"'.('/<[*-_<+>?}{>]@}+@}]])@+}@<[*-_<+>?}{>]@^'^'`^=_^<]_[[]+[/,]_/]-/@._]/^=_^<]_[[]+[/,|').',$/})')

It prints "Obfuscated Perl to print obfuscated Perl"

I want to know how it actually prints this.

Kathaleenkatharevusa answered 26/7, 2016 at 13:57 Comment(0)
C
10

It is making good use of the bitwise string XOR operator ^.

']])@+}' ^ '-/@._]'

evaluates to print,

'/<[*-_<+>?}{>]@}+@}]])@+}@<[*-_<+>?}{>]@^' 
    ^ '`^=_^<]_[[]+[/,]_/]-/@._]/^=_^<]_[[]+[/,|'

evaluates to Obfuscated Perl to print obfuscated Perl" and the whole program reduces to

$ perl -MO=Deparse ...
'' =~ m[(?{print "Obfuscated Perl to print obfuscated Perl",$/})];
... syntax OK

Related: Acme::EyeDrops

Capsaicin answered 26/7, 2016 at 14:12 Comment(3)
I still didnt get how it works :( it would be nice if you add detailed explanation for ']])@+}' ^ '-/@._]'Kathaleenkatharevusa
Follow the link. The ordinal value of each character in the first string is XOR'd with the ordinal value of the corresponding character in the second string and converted back to a character. ord("]") is 93, ord("-") is 45, 93 xor 45 is 112. chr(112) is pCapsaicin
Note that you need use re 'eval'; with 5.24 and later.Sleuth

© 2022 - 2024 — McMap. All rights reserved.