Well, the code actually wasn't intended to be obfuscated. I've never had much of an interest in writing obfuscated perl, and haven't read the node you mentioned. (Although I expect I will as soon as I'm done posting my reply....)

I started with the mere idea that I wanted to write "My Bonnie Lies Over the Ocean" as a perl poem, and just tried my hardest to force the poem to be syntactically valid. I will admit that it turned out better than I expected. Here's a collection of thoughts that might help to explain how it got into its final form.

I started with:

my $bonnie = "lies_over_the_ocean"; my $bonnie = "lies_over_the_sea"; my $bonnie = "lies_over_the_ocean";
Of course, the compiler complained about multiple declarations of $bonnie within the same scope, so I merely added the requisite braces to make each $bonnie declaration fall into a new scope.

The 0; before the first bring_back() is simply a no-op. It's there because the actual song contains the word "oh" at that point.

I noticed that the words "bring back" are used repeatedly throughout the song, and made an analogy between lyrics and code. As a rule, oft-used pieces of code should be stuck within a function. Hence, I created a bring_back() function and called it repeatedly. It just "worked out" to have nested calls to bring_back(), since that fits the flow of the actual song. The calls to bring_back() have the added advantage of setting up new scopes such that I can declare $bonnie again. The 0,'s within the function calls continue the tradition set up earlier; they use the number 0 to represent the phonetic "oh"'s that are present in the real song, while keeping the code syntactically valid.

I called each bring_back() with whatever arguments fit the song.

At this point, I created an empty sub bring_back {} that did nothing, and thought my work was done.

I then decided it would be nice for the poem to actually output something, and decided that bring_back() should be the place to do it. The obvious problem was getting the output to print only once, since bring_back() gets called 7 times. I decided I'd use the number of arguments passed to bring_back() as the criteria for printing, and simply toyed with bring_back()'s return value until I got a unique number of arguments passed (5) on the last call.


In reply to Obfuscation? by Falkkin
in thread my $bonnie by Falkkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.