The Little Conveniences

| 5 Comments

My favorite new feature in Perl 5.10.0 was say. How silly is that? say works like this:

use feature 'say';

# replace this
print "This is some text!\n";

# with this

say 'This is some text';

All it does it add a newline to the end of the printed line. Yes, it's two characters shorter, but it does almost nothing... except that it's two characters shorter, it adds behavior you add most of the time, it lets you avoid unnecessary interpolation, and did I mention that \n is actually a little bit ugly in a string?

I liked it so much from Perl 6, I implemented it in Parrot for the four fundamental datatypes.

I liked it so much, I wrote Modern::Perl to enable it (and everything else I like in Perl 5.10) with a single command.

As silly as that all sounds, I've heard from other people I trust that this single little feature is almost indispensible. It's a tiny convenience. It's a small thing. Yet once you acclimate to its presence, its simplicity, its usability, its utility, you appreciate what it does.

The same goes for Moose, or MooseX::Declare, or other convenience mechanisms that are, on their face, purely syntax. Syntactic sugar gets a bad reputation when it obfuscates the necessary, but it doesn't get enough credit when it illuminates the important by removing the unnecessary.

That's why I so dislike the default argument against adding useful and necessary syntax. It's so very easy to say "A class is just a package and a method is just a function." It's true, and yet it's so trivial, as if the Church-Turing equivalence were solely an argument against any new syntax: a branch is just a goto, a lexical variable is just a store in an offset, and function parameters were merely a list on a stack somewhere you can unpack yourself in any fashion you like.

Yes, that's all true, but how so much does it miss the point.

Sometimes a little bit of syntax can make your program simpler, more beautiful, more elegant, more maintainable, and more enjoyable. After all, shouldn't you seek to enjoy programming?

5 Comments

On another note, the Perl community so far doesn't think the convenience of *optional* newline output (e.g. Ruby's puts()) is worth any salt.

While I agree completely that syntactic sugar is not to be overlooked, MooseX::Declare has a number of problems. It relies on PPI to parse the method signatures, rather than perl. While it may work, this seems like a hack to me. Not only is the dependency chain huge and difficult to install, but a startup time of 5 seconds is not unusual. Even if you're not writing command-line tools, this can end up being annoying when trying to do test driven development, as your test script runs a lot longer than is necessary.

On top of that, the error messages it generates are unintelligible, and unparseable. I've also seem some weird compile time issues (i.e. a compile error not generating the correct error message), especially when using Module::Pluggable (but haven't been able to isolate them to report a bug).

I recently replaced some code using MooseX::Method::Signatures (which MooseX::Declare uses for its method syntax) with MooseX::Params::Validate. Syntactically, it's nowhere near as nice, but functionally I lose nothing.

As with anything, these things are a trade-off. In my opinion, the problems with MooseX::Declare outweigh the syntactical gains... and I've been rather alarmed to see parts of the community rush to embrace it despite this.

The question of quality of implementation is very different! Some things are best done in core, but first you have to convince other people that those things are worth doing anywhere.

Yes, fair enough. I don't have any problems with people exploring ideas, and I'd love to see the syntax the MooseX::Declare introduces available and working well. I just think these shortcomings should be more clearly advertised.

I too find say simple but sweet, since I get weary of typing print 'string', "\n";. However, it's a pity it took so long to become a part of the language. I'm stuck in 5.8.x at work so I don't get to say. Working with legacy code and 5.8.x can be depressing enough that at times I have to "cleanse myself" writing modern Perl on the side and that includes liberal doses of say.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

affiliated with ModernPerl.net

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on January 25, 2011 2:09 PM.

What Else a Method Keyword Could Do was the previous entry in this blog.

Your EDSL is only Pretty in Stockholm is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Sponsored by Blender Recipe Reviews and the Trendshare how to invest guide

Powered by the Perl programming language

what is programming?