Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: my $bonnie

by t'mo (Pilgrim)
on Jan 14, 2001 at 09:28 UTC ( [id://51699]=note: print w/replies, xml ) Need Help??


in reply to my $bonnie

I'm curious... Could you share your thought process as you wrote this?

I've looked at Writing highly obfuscated code in Perl, but most of the tips* found in that node are not demonstrated in this snippet. Some obfuscations demonstrate a certain level of style and creativity; yours is one example.

* I would say most of those tips, used alone and without creativity, would in fact be nothing more than cheap tricks. Not unlike playing a musical instrument well technically, but without making any music.

Replies are listed 'Best First'.
Obfuscation?
by Falkkin (Chaplain) on Jan 14, 2001 at 11:33 UTC
    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-12-09 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (53 votes). Check out past polls.