perlmeditation
Mutant
I was reading an old [id://487741|post] by [gargle], which links to the <a href="http://www.catb.org/~esr/jargon/html/index.html">Jargon File</a>. Naturally, I went to the entry for <a href="http://www.catb.org/~esr/jargon/html/P/Perl.html">Perl</a>, where I read:
<blockquote>
Though Perl is very useful, it would be a stretch to describe it as pretty or elegant; people who like clean, spare design generally prefer Python.
</blockquote>
I don't really like to participate in language wars, but I've always considered Perl to be one of the more elegant languages. Browsing to the Jargon File's definition of <a href="http://www.catb.org/~esr/jargon/html/E/elegant.html">elegant</a>, I found:
<blockquote>
Combining simplicity, power, and a certain ineffable grace of design.
</blockquote>
(Which, in my opinion, could be a Perl motto!)
<br><br>
Elegance in programming to me is about expressing actions (commands, statements, etc.) in a simple and coherent way. Perl's TIMTOWTDI-ness is (to me) the ideal tool for this.
<br><br>
One example for me in Perl is <code>map</code>. If I have a list of things, and I want to transform them all in some (fairly simple) way, it makes sense to do it all in one line. That is, I would define:
<code>
@foo = map { '%'.$_.'%' } @foo;
</code>
as more elegant than:
<code>
my @new_foo;
foreach my $a_foo (@foo) {
$a_foo = '%'.$a_foo.'%';
push @new_foo, $a_foo;
}
@foo = @new_foo;
</code>
I'm not just talking about reducing line count, however. If we were doing 10 different things to the items in @foo, and some of them depended on what the individual item was, it would be inelegant to use map, and more elegant to use foreach.
<br><br>
Still, I feel like there's much more to elegant code. There's certainly a "prettiness" factor. I sometimes just have a feeling that this is the "right" way to do it. And I can't really define it any more scientifically than that.
<br><br>
To me Perl allows us to be elegant, simply by offering us many different ways to do something. Of course, this also gives us the opportunity to be even less elegant than other languages, but that seems to be the trade-off with many things in Perl.