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.