Output with say
Perl 5.10 borrows the say
built-in from the ongoing development of Perl 6 (which may have borrowed
its say from Pascal’s println). It’s the same as print, although it adds a newline to the end.
These forms all output the same thing:
use 5.010; print "Hello!\n"; print "Hello!", "\n"; say "Hello!";
To just print a variable’s value followed by a newline, you don’t
need to create an extra string or print a list. You just say the variable. This is especially handy in
the common case of simply wanting to put a newline after whatever you
want to output:
use 5.010; my $name = 'Fred'; print "$name\n"; print $name, "\n"; say $name;
To interpolate an array, you still need to quote it though. It’s the quoting that puts the spaces between the elements:
use 5.010; my @array = qw( a b c d ); say @array; # "abcd\n" say "@array"; # "a b c d\n";
Just like with print, you can
specify a filehandle with say:
use 5.010; say BEDROCK "Hello!";
Since this is a Perl 5.10 feature though, we’ll only use it when
we are otherwise using a Perl 5.10 feature. The old, trusty print is still as good as it ever was, but we
suspect that there are some Perl programmers out there who will want the
immediate savings of not typing the four extra characters (two in the
name and the \n).