Why I love say

Dean Wilson reveals My Favourite Three Features in a recent blog post, but doesn't discuss my #1 feature of Perl 5.10: The say command.

Why love say so much? It's just the same as print with a "\n" at the end, right? Yup, but that "\n" causes heartache for me in life. See, I've been working on removing interpolation from my life wherever possible. For instance, we've probably all seen beginners do something like:

some_func( "$str" );

where the quotes around $str are unnecessary. (Yes, I know there could be overloaded stringification, but I'm ignoring that possibility here.) That function call should be done as:

some_func( $str );

By the same token, I don't use double quotes any more than necessary. Rather than creating a string as:

my $x = "Bloofga";

do it as

my $x = 'Bloofga';

It's not about speedups. It's about not making the code do anything more than it has to, so that the next programmer does not have to ask "why is this work getting done?" If the code doesn't need the double-quoting, then don't use the double-quoting.

I started down this road when I read this rule in Perl Best Practices, but ignored it. "Eh, no biggie," I thought. Then I started using Perl::Critic, and it complained about everywhere I was using double quotes. As I examined those complaints, I came around to realize that if you're having the computer do work, the next programmer has to wonder why.

So now we get the say command, and I get to eliminate at least 50% of my necessary string interpolation. Instead of:

print "Your results are:\n";

I can now use:

say 'Your results are:';

So much cleaner. In a color-coding editor like vim, the distinction is even clearer, and as MJD likes to point out, "It's easier to see than to think."

Start using say. Even if you're not on 5.10 yet, you can use Perl6::Say for most of the places that say works in 5.10. Even better, stop using unnecessary interpolation altogether.



Not to disagree with your sentiment, and noting that you say "It's not about speedups" but I thought it worth pointing out that if no interpolation is required, the perl compiler will treat " as '. eg:

$ perl -MO=Deparse -e "print 'hi'"
print 'hi';
-e syntax OK

is the same as:

$ perl -MO=Deparse -e 'print "hi"'
print 'hi';
-e syntax OK

I like using '' where interpolation is not needed because it avoids problems where I inadvertently edit a string and add in an $ or @ and intend it to be literal.

Sure, it's a dumb mistake and it's easily caught or corrected when it is made, but if you just get in the habit of reserving "" for when it's needed you don't make that mistake.

Leave a comment

About this Entry

This page contains a single entry by Andy Lester published on January 1, 2008 2:17 PM.

Update your copies of DateTime::TimeZone was the previous entry in this blog.

Superstition has no place in programming is the next entry in this blog.

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

Other Perl Sites

Other Swell Blogs

  • geek2geek: An ongoing analysis of how geeks communicate, how we fail and how to fix it.