At last week’s Philadelphia Perl Mongers meeting, I asked what’s new and exciting in Perl since I last used it exclusively, circa Perl 5.6. Walt chimed in with say, a command that prints an expression and adds a new line at the end. New and exciting?
Other languages have always had separate commands to display strings with or without newlines at the end. Embedding escape codes like \n or using Perl’s smarts around concatenating and contextualizing work fine, so a separate command isn’t necessary like in some of those other languages with print and println or write and writeln. “So why now?” I asked.
Some of the semi-glib responses to my follow-up touched on the venerated trait of laziness among Perl programmers and joking about doing more with less (i.e., two less characters from print to say). I say semi-glib because both of these comments hold kernels of truth about Perl that originally drew me to the language and explain my continued frustration with the “newer” languages that I now have to call bread and butter.
Perl has always been a programming language for the lazy, proud, and impatient. From personal experience, I’m more likely to use print with a “\n” than without, so that little extra work spread out over the thousands and thousands of print “…” does add up. There is some sense in having a command whose default mode includes a linefeed.
Perl has also always been a language about packing–functionally and semantically. That’s earned it a reputation of being hackish or too clever for itself, but anything taken to an extreme can be bad. My experience supports the perlish idea that less code written is less code to debug or relearn later. Some languages, especially those fond of methods on literal strings instead of operators, provide flexibility at the cost of verbosity and ugliness. The idea of packing more capability into two less characters is very, very Perl.
There’s talk of trying to reinvigorate the Perl base and recover some of the mindshare (and subsequent marketshare) that Perl’s lost over the last decade. I don’t think say will convince legions of .Net or Java programmers to switch, but I’ll definitely use it my next script.
From the Perldocs on say:
- say FILEHANDLE LIST
- say LIST
- say
Just like print
, but implicitly appends a newline. say LIST
is simply an abbreviation for { local $\ ="\n"; print LIST }
.
This keyword is available only when the “say” feature is enabled: see feature.