Umair Ayub
Umair Ayub

Reputation: 21241

How does this Perl code work?

I found this Perl program:

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

It prints "Obfuscated Perl to print obfuscated Perl"

I want to know how it actually prints this.

Upvotes: 9

Views: 202

Answers (1)

mob
mob

Reputation: 118605

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

Upvotes: 10

Related Questions