Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Writing highly obfuscated code in Perl

by jonadab (Parson)
on Apr 19, 2005 at 11:41 UTC ( #449196=note: print w/replies, xml ) Need Help??

in reply to Writing highly obfuscated code in Perl

Some additional tips...

  • First, think of something interesting to do. Second, think of an unobvious way to accomplish it. Then start writing the code. With an obfuscated algorithm from the get-go, and code that does something interesting, your obfu will be sure to please.
  • Avoid using
    if (condition) { foo; } else { bar; }
    when you can instead use the trinary operator: (condition)?foo:bar;. This is especially important when you have nested conditionals.
  • Try to model parts of your code around paradigms that a lot of people aren't familiar or comfortable with, such as functional or logical programming. Combine these with often-poorly-understood data structures if possible. For instance, having map generate a hashref of closures each iteration is sure to confuse at least some of the people reading your code.
  • Include POD that is simultaneously cryptic and actively misleading.
  • Play strange games with context and precedence.
  • Abuse symbolic references.
  • Include at least one red herring, but make sure that it is just as obfuscated and hard to follow as the rest of the code, so that it will not be immediately written off as a red herring.
  • While writing your code, go ahead and use obfuscatory techniques, but keep some line breaks and comments in so you can keep track of what you're doing; once you get it working just the way you want and nicely obfuscated, then remove the comments, golf it down a little, and alter the whitespace to reform the code into the shape you want; test after every couple of changes to make sure it still does what you want.
  • Don't break up into subroutines at the natural places; leave things inline that in real code would be better broken out as subroutines, and instead break things out as subroutines that would be better off inline in real code. Make sure your subroutines do things with the caller's variables, if possible.
  • Try to use hashes and arrays with the same single-character names as the scalars you are using, especially any punctuation-named special variables you are abusing.
  • When using regular expressions, don't use regular /slashes/ as delimiters; find more interesting characters for that, preferably ones that are also used for other things both within the regexen (where they will have to be escaped) and near them in the code.
  • Abuse the /e modifier with regex substitutions.
  • Nested string eval can be fun; combine it with $_ and substitutions to create self-modifying code.
  • Use things for multiple unrelated purposes. For instance, if you have an array or hash that you happen to know contains some elements, you can grab it in scalar context any time you need a true value. Similarly, any variable that you know contains a non-numeric string can be used as zero in numeric context. Apply this principle as often as possible, in as many different ways as possible.
  • Converting foreach and even while loops into map will often shave off several characters, with the added benefit of allowing you to assign the result to an array (or hash!) which you can subsequently use as a red herring, a true value in boolean context, or even as a number.
  • Golf, but don't overgolf. Frequently the absolute shortest way to say something is not the most confusing.

  • "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://449196]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-10-01 01:11 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (172 votes). Check out past polls.