Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

my $bonnie

by Falkkin (Chaplain)
on Jan 13, 2001 at 14:12 UTC ( #51585=poem: print w/ replies, xml ) Need Help??

Adapted from the folk song, of course. Compiles and runs even with warnings and strict, which is always nice.
#!/usr/bin/perl -w use strict; my $me; my $bonnie = "lies_over_the_ocean"; { my $bonnie = "lies_over_the_sea"; { my $bonnie = "lies_over_the_ocean"; { 0; bring_back(my $bonnie = 2, $me); { bring_back( bring_back( 0, bring_back(my $bonnie = 2, $me, 2, $me )));{ bring_back( bring_back( 0, bring_back(my $bonnie = 2, $me )))}}}}} sub bring_back { print "brought back my bonnie!\n" if @_ == 5; return ("",@_); }
Update: As per chipmunk's suggestion, I've modified the poem such that it runs under archaic versions of perl. ;)

Comment on my $bonnie
Download Code
Re: my $bonnie
by chipmunk (Parson) on Jan 13, 2001 at 20:37 UTC
    Very nice! You've got me singing along to your poem.

    One suggestion; if you turn on warnings with -w instead of use warnings, your poem will run under 5.005 too. :)

Re: my $bonnie
by t'mo (Pilgrim) on Jan 14, 2001 at 09:28 UTC

    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.

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: poem [id://51585]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-07-12 16:45 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (240 votes), past polls