|No such thing as a small change|
Zen And The Art Of Perl Poetryby webfiend (Vicar)
|on May 30, 2002 at 19:21 UTC||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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
"All you need is ignorance and confidence; then success is sure."-- Mark Twain