Now that I've got Snow Leopard (finally) installed on my Mac, the default perl binary is now 5.10.0. This means many things: The given keyword and smart matching, the defined-or operator, the wonderful additions to the regex engine, and other things I'm bound to blog about later when I get round to enthusing about them.
What I wanted to talk about today is the simpliest change that'll be making the most difference to me on a day to day basis: The "say" keyword. More or less
say
is exactly the same as
print
but two characters shorter and automatically adds a newline at the end. This is most useful when you're writing one liners. This quick calculation:
bash$ perl -e 'print 235*1.15, "\n"'
Becomes just:
bash$ perl -E 'say 235*1.15'
(Note the use of
-E
instead of
-e
to automatically turn on the 5.10 keywords like
say
without having to add
use 5.10
or
use feature 'say'
.)
This saves us a grand total of nine keypresses (including having to hit shift one less time.) More importantly it saves us having to use double quotes at all. This is really useful when you're already using the quotes for something else. For example, running a Perl one-liner remotely with ssh:
bash$ ssh me@remote "perl -MSomeModule -e 'print SomeModule->VERSION, qq{\n}"'
With 5.10 on the remote machine this becomes just:
bash$ ssh me@remote "perl -MSomeModule -E 'say SomeModule->VERSION'"
This has not only the advantage of saving me a bunch of keystrokes, but also doens't make me think as much. And the less I have to think, the less chance I'm going to do something stupid and make a mistake.