Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

... or ...

TMTOWTWPP: There's More Than One Way To Write Perl Poetry.

... or ...

webfiend obviously drinks too much coffee

I'm not sure if this is a study of Perl syntax, an adventure in poetry, or just a disturbing insight into the way my brain works. I just hope it is in some way enlightening to the Perlmonks community or the psychiatric community.

Let us imagine a situation where we are trying to explain the richness of Perl's syntax to somebody who has never worked with the language before. This imaginary person has a rare but ideal trait: she is not emotionally invested in the programming languages she does use. Because of this, she does not automatically believe Perl is bad because it has rich syntax. She just wants to see what you mean. Perl poetry is your friend in this circumstance.

How about a situation a little less imaginary? You just want to fiddle around and see how many ways you can write the same poetry in Perl. Yeah, I'll go with this one.

Let's go with a simple adage, to keep the possibilities from overwhelming us. One of my favorite tidbits is this old Zen piece that I first came across in an Alan Watts book. It's short, to the point, and easy to express. It'll be a while before I can express the sound of one hand clapping in perl.

If you understand, things are such as they are.
If you do not understand, things are such as they are.

The trick with Perl poetry, as with any poetry, is figuring out how you want to say it. We could get away with just throwing in some perl code in a fashion that resembles the poetry we're thinking of. I've done it plenty of times myself. Good poetry follows rules, though (or at least knows what rules it's breaking). How about this for the basic rule of Perl poetry?

"Good" Perl poetry must be able to survive a pass from perl -c or perl -e.

I think that already is the guideline, judging from some of the better poetry I've seen on Perlmonks. Still, I like to get this stuff out in the open.

You might notice that there is nothing in there about rhyming, iambic pentameter, or use warnings. It's poetry, for crying out loud! We can be loose with the rules if we want. And if my poetry had to rhyme or have a meter, all you'd ever see from me would be limericks.

$youngMan = YoungMan->new( -from => "Nantucket" );

No. I don't think so. Let's get back to our Zen adage, shall we?

Your vision might be different, but to me this looks like a two-way if switch that assigns a value to an array called things based on your understanding. Never mind that the value assigned is the same either way. We'll get to that in a moment.

if ($you{understand}) { @things = qw(such as they are); } else { @things = qw(such as they are); }

That was too easy. We could be strict and make sure that everything is properly declared, like the good programmers we want to be. It also occurs to me that "such as they are" is the same as assigning the value that @things already had. Plus, the adage is very specific in it's selection structures. This if you understand, that (or rather this again) if you do not understand. There is the slight possibility of some gray area where you sort of understand, but not really, and the state of @things is not specified for that condition.

use strict; my %you = ( understand => rand(2) % 2 ); our @things; if ($you{understand}) { @things = @things; } elsif (not $you{understand}) { @things = @things; }

Poetry that compiles under use strict. I'm proud of myself. But I don't think Zen really works that way. It occurs to me that a philosophy concerned about the sound a lonely falling tree makes might be better expressed by using less, rather than more, syntax. So let's tighten it up a bit with modifier expressions.

@things = @things if $you{understand}; @things = @things if not $you{understand};

I've sort of reversed the expressions for the sake of brevity, but it's still the same statement. Can I tighten it up even more? Of course I can.

@things = ($you{understand}) ? @things : @things;

We've lost the fuzziness of the expression for the sake of brevity. I'm on a roll, though, so don't try to stop me now. The adage seems to be telling us that things are things regardless of our understanding, so the selection structures are irrelevant.

@things = @things;

Now I look at it from the perl execution side of things, and I see that this statement does nothing to the value of @things.It does, however, return the value of things. I think we can express that, and it'll still keep to the whole Zen kick we got going on.

@things;

An evil thought just occurred to me. Since @things is uninitialized, that line doesn't really return anything. I'm not sure, but I think the following is as Zen as you can get with Perl:

Missed it, didn't you? Okay, let's try it as a one-liner.

$ perl -e ''

That might be the sound of one hand clapping, too. I think the only way you can get more Zen with Perl is just to sit there and think about void contexts.

There you have it. Seven ways to express the same thing in Perl. Eight, if you count the one-liner. Granted, the last few are a bit nonsensical. Hopefully our imaginary friend got the point, though. Perl is a rich language, more than capable of writing poems. Or koans, as the case may be.

Updates:

  • Missed a "greater-than" in a couple of the code blocks (plus another one pointed out by jeffa).
  • Some grammatical suggestions to my caffeine-addled brain from jeffa


"All you need is ignorance and confidence; then success is sure."-- Mark Twain

In reply to Zen And The Art Of Perl Poetry by webfiend

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-28 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found