<?xml version="1.0" encoding="windows-1252"?>
<node id="170492" title="Zen And The Art Of Perl Poetry" created="2002-05-30 15:21:46" updated="2005-08-14 17:55:14">
<type id="1584">
poem</type>
<author id="8157">
webfiend</author>
<data>
<field name="doctext">
&lt;p&gt; ... or ...&lt;/p&gt;

&lt;h4&gt;TMTOWTWPP: There's More Than One Way To Write Perl Poetry.&lt;/h4&gt;

&lt;p&gt; ... or ...&lt;/p&gt;

&lt;h4&gt;[webfiend] obviously drinks too much coffee&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;blockquote&gt;
If you understand, things are such as they are.&lt;br&gt;
If you do not understand, things are such as they are.
&lt;/blockquote&gt;

&lt;readmore&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;blockquote&gt;
"Good" Perl poetry must be able to survive a pass from &lt;code&gt;perl -c&lt;/code&gt; or
&lt;code&gt;perl -e&lt;/code&gt;.
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You might notice that there is nothing in there about rhyming, iambic
pentameter, or &lt;code&gt;use warnings&lt;/code&gt;.  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.&lt;/p&gt;

&lt;code&gt;
$youngMan = YoungMan-&gt;new( -from =&gt; "Nantucket" );
&lt;/code&gt;

&lt;p&gt;No.  I don't think so.  Let's get back to our Zen adage, shall we?&lt;/p&gt;

&lt;p&gt;Your vision might be different, but to me this looks like a two-way
&lt;code&gt;if&lt;/code&gt; switch that assigns a value to an array called
&lt;code&gt;things&lt;/code&gt; based on your understanding.  Never mind that the value
assigned is the same either way.  We'll get to that in a moment.&lt;/p&gt;

&lt;code&gt;
if ($you{understand}) {
    @things = qw(such as they are);
}
else {
    @things = qw(such as they are);
}
&lt;/code&gt;

&lt;p&gt;That was too easy.  We could be &lt;code&gt;strict&lt;/code&gt; 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 &lt;code&gt;@things&lt;/code&gt; already had.  Plus, the adage is very
specific in it's selection structures.  &lt;em&gt;This&lt;/em&gt; if you understand,
&lt;em&gt;that&lt;/em&gt; (or rather &lt;em&gt;this&lt;/em&gt; 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 &lt;code&gt;@things&lt;/code&gt; is not specified for that
condition.&lt;/p&gt;

&lt;code&gt;
use strict;

my %you = ( understand =&gt; rand(2) % 2 );
our @things;

if ($you{understand}) {
    @things = @things;
}
elsif (not $you{understand}) {
    @things = @things;
}
&lt;/code&gt;

&lt;p&gt;Poetry that compiles under &lt;code&gt;use strict&lt;/code&gt;.  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.&lt;/p&gt;

&lt;code&gt;
@things = @things if $you{understand};
@things = @things if not $you{understand};
&lt;/code&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;code&gt;
@things = ($you{understand}) ? @things : @things;
&lt;/code&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;code&gt;
@things = @things;
&lt;/code&gt;

&lt;p&gt;Now I look at it from the &lt;code&gt;perl&lt;/code&gt; execution side of things,
and I see that this statement does nothing to the value of &lt;code&gt;@things&lt;/code&gt;.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.&lt;/p&gt;

&lt;code&gt;
@things;
&lt;/code&gt;

&lt;p&gt;An evil thought just occurred to me.  Since &lt;code&gt;@things&lt;/code&gt; 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:&lt;/p&gt;
&lt;code&gt;

&lt;/code&gt;

&lt;p&gt;Missed it, didn't you?  Okay, let's try it as a one-liner.&lt;/p&gt;

&lt;code&gt;
$ perl -e ''
&lt;/code&gt;

&lt;p&gt;That &lt;em&gt;might&lt;/em&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Updates:&lt;/b&gt;&lt;ul&gt;
&lt;li&gt;Missed a "greater-than" in a couple of the code blocks (plus another one pointed out by [jeffa]).&lt;/li&gt;
&lt;li&gt;Some grammatical suggestions to my caffeine-addled brain from [jeffa]&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;

&lt;hr&gt;
&lt;blockquote&gt;"All you need is ignorance and confidence; then success is sure."-- Mark Twain&lt;/blockquote&gt;</field>
</data>
</node>
